Is currying possible in crystal

Hi is currying possible in crystal?

Yes! Well, more or less. I guess it all depends on what you want from currying. Like, there’s no automatic currying like in Haskell or Elm. But, you can get a reference to a method, and then “curry” it (I’m not sure if there’s a difference between that and partial application)

Here’s an example:

# This is a function that adds two things
def add(x, y)
  x + y
end

# With this we get a function that adds two integers
add_function = ->add(Int32, Int32)
p! add_function.call(1, 2)

# Now we get the add function but with the first argument fixed to 10
add10 = add_function.partial(10)
p! add10.call(5)

You can see it working here.

That said, even though it exists, I never used that in my life, not in Crystal and not in any other language that’s not Haskell or Elm.

4 Likes

Aa ok i see. Well for reference i was looking at this implementation of parser combinators:

I thought it was interesting and thought it might be interesting to try replicating in crystal. But it uses partial application in js. This was the context.

Will play around with the suggested approach. Ir maybe ill find a better approach.

I’d love to see what you get in the end :-)

We built something like this for some of our projects

4 Likes

Really nice! I started working on something like that, for fun, and it seems that projects ticks all the things I wanted and more!

It would be great to have an examples directory with some example parsers, like a math calculator, or even basic JSON.

I most recently used it for unit conversion and parsing

which is pretty complex, the spec probably doesn’t do it justice

still have a todo on parsing grouped expressions there, but it has the basics down