I’m trying to get the hang of partial application, what I have so far would be something like, (define (multiply-by x)
(λ (y) (* x y)))
(define by-2 (multiply-by 2))
(by-2 3)
Is this the right way of doing partial application in Racket or is there another way?
Yes. It’s the right way. An anternative: (define ((multiply-by y) x) (* x y)) (hope I got the syntax right)
I prefer your way though.
Cool, thanks, glad I’m on the right lines.
Once you get the hang of it, you can use curry
: (define by-2 (curry * 2))
(by-2 3)
I was going to try and get my head round currying next.
it does pretty much what you do in the background :slightly_smiling_face:
Looks that way, very neat.
there’s also something I use sometimes and wish I would have discovered sooner: cut
(require (only-in srfi/26 cut))
(define (create-user name age score)
(list 'user name age score))
(define create-old-user (cut create-user <> 90 <>))
(create-old-user "George" 1800)
=> '(user "George" 90 1800)
The only drawback is that cut
is a syntax while curry
is a procedure
Wow, that’s blown my mind.
Sort of placeholder parameters?
yep
Neat
There’s usually other ways to do it, but sometimes it’s handy
Yes, there does always seem to be many ways of doing anything :grinning:
If you work in a team, I guess cut
is a bit more magic and cryptic than just making a named procedure, but it really depends on the culture of the working group.
I’d say cut
is doomed to disappear quickly anywhere performance is at stake.
The more “Racketty” way would be to use keywords, I guess: (define (create-user #:name [#f name] #:age [#f age] #:score [0 score]
(list 'user name age score))
(define (create-old-user name score)
(create-user #:name name #:age 90 #:score score))
The really handy part about cut
is that is allows to easily use a library in which parameters are not in a practical order
for “glue code” it’s perfect
@jerome.martin.dev Thanks for that.
@jerome.martin.dev personally, rather than cut
I like fancy-app
package (of course, I did write it): https://github.com/samth/fancy-app/
@samth Nice one, is that similar to another language? I seem to have seen that sort of syntax before. Clojure maybe, I can’t remember.
@samth It’s 46 lines of goodness :smile:
it’s inspired by scala (the clojure one is a bit different)
Of course, I didn’t read the top line.
threading
is one inspired more by Clojure
“Functional programming” is a paradigm where people explore all possible ways to not-supply parameters to pure functions. :stuck_out_tongue:
Hehe
curly-fn
also exists, and it provides a Clojure-style function shorthand.
(It’s my package.)
Ah, so many choices.