Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 11:30
    straight-shoota milestoned #11503
  • 11:30
    straight-shoota milestoned #11538
  • 11:29
    straight-shoota milestoned #11145
  • 10:27
    oprypin review_requested #11230
  • 10:18
    oprypin review_requested #11536
  • 10:15
    oprypin review_requested #11162
  • 10:15
    oprypin review_requested #11094
  • 10:14
    oprypin review_requested #11242
  • 10:13
    oprypin review_requested #11242
  • 10:13
    oprypin review_requested #11268
  • 10:12
    oprypin review_requested #11450
  • 06:58
    HertzDevil labeled #11538
  • 06:58
    HertzDevil labeled #11538
  • 06:58
    HertzDevil labeled #11538
  • 06:58
    HertzDevil opened #11538
  • 03:59
    HertzDevil labeled #8413
  • 03:59
    HertzDevil labeled #8413
  • 03:59
    HertzDevil labeled #8413
  • 03:57
    HertzDevil closed #6510
  • 03:55
    HertzDevil labeled #6373
George Dietrich
@Blacksmoke16
the former should work if you just do yield elt
iirc with .. yield is kinda buggy in some cases
so :shrug:
mfiano
@mjfiano:matrix.org
[m]
Pretty nifty, it works
This will do until I macroize things, thanks
George Dietrich
@Blacksmoke16
:thumbsup:
mfiano
@mjfiano:matrix.org
[m]
I'm not quite sure how I'd call element_wise for this one though, as it takes a second argument:
    def copysign(other : self)
      x = Math.copysign @x, other.x
      y = Math.copysign @y, other.y
      V2.new x, y
    end
George Dietrich
@Blacksmoke16
prob just keep it as is?
mfiano
@mjfiano:matrix.org
[m]
There's a bunch like that. They all have the same pattern, just additional args
George Dietrich
@Blacksmoke16
id say just go with a macro imo
even if you have a helper private method its still just boilerplate
mfiano
@mjfiano:matrix.org
[m]
I'm feeling a little intimidated by Crystal macros still
Spent last night trying to figure out the basics from reading the docs...couldn't apply it to my own code
George Dietrich
@Blacksmoke16
{% for m in ["round", "abs"] %}
    def {{m.id}}
      v = dup
      v.each_with_index { |elt, i| v[i] = elt.{{m.id}} }
      v
    end
{% end %}
assumes the method name is the same as the method called on the element, but could easily work around that if thats not what you want
mfiano
@mjfiano:matrix.org
[m]
In my case, I'd only want to generate the method body, not the whole method, supplying the name of the method called on the element
and therefor without a hard-coded list of strings too
George Dietrich
@Blacksmoke16
why would it matter?
mfiano
@mjfiano:matrix.org
[m]
@Blacksmoke16: I took your advice 😀
George Dietrich
@Blacksmoke16
:thumbsup:
mfiano
@mjfiano:matrix.org
[m]
George Dietrich
@Blacksmoke16
hmm
i have an idea
mfiano
@mjfiano:matrix.org
[m]
This is intended to be included on all 3 of my vector types. I may split it up into 2 mixins, for the methods that don't make sense for matrices
George Dietrich
@Blacksmoke16
private macro element_wise(name, &)
  def {{name.id}}
    out = dup
    each_with_index { |elt, idx| out[idx] = {{yield}} }
    out
  end
end

element_wise fract do
  elt - elt.floor
end
I think that should work
or even element_wise frac { elt - elt.floor }
mfiano
@mjfiano:matrix.org
[m]
Idea is to try to get rid of all references to @x, @y, etc so it's DRY for the related types
Oh, lemme read
George Dietrich
@Blacksmoke16
also fwiw out is a keyword so prob wouldn't be a bad idea to pick something else
technically works, but going to mess up highlighting
mfiano
@mjfiano:matrix.org
[m]
I mean, that doesn't save any code by expanding into a toplevel definition, and imho isn't as easy to scan the file
George Dietrich
@Blacksmoke16
:thinking: how do you figure that doesn't save any code?
mfiano
@mjfiano:matrix.org
[m]
re: out: ok
9-11 are 3 lines. so is 24-26 in mine?
George Dietrich
@Blacksmoke16
  def sign
    element_wise elt.sign.to_f
  end

  def copysign(other : self)
    element_wise Math.copysign elt, other[idx]
  end

  def fract
    element_wise elt - elt.floor
  end
Gets turned into:
element_wise sign { elt.sign.to_f }
element_wise copysign { Math.copysign elt, other[idx] }
element_wise fract { elt - elt.floor }
mfiano
@mjfiano:matrix.org
[m]
I could just as well do that with mine since they're short, albeit a bit differently
The benefit is I instantly know they are defs, and syntax highlighting gives my brain less to process when quickly scanning a source file
Correct me if I'm wrong :)
George Dietrich
@Blacksmoke16
fair enough
mfiano
@mjfiano:matrix.org
[m]
But I'll think about it. You have given me nothing but good suggestions this week and I am a bit tired now and about to quit, so we'll see. Thank you
George Dietrich
@Blacksmoke16
o/
on an unrelated note, https://play.crystal-lang.org/#/r/blio is this output expected?
looks to be a regression in 0.31.0
Matthew Berry
@mattrberry
Out of curiosity, are there downsides to a type system that supports something like this? https://carc.in/#/r/bljn
Quinton Miller
@HertzDevil
From IRC (bridge bot)
@FromIRC
<raz> if foo = bar should be a syntax error
From IRC (bridge bot)
@FromIRC
<raz> i keep seeing it in crystal code used as a workaround for .try and .not_nil!. people want their code to be fluent to read, but replacing warts with traps is not a solution :⁠(
Quinton Miller
@HertzDevil
they are fluent to read unless you come from c
even in c++ if (auto y = dynamic_cast<Derived *>(x)) is perfectly fine and fluent