mark.warren
2018-11-20 09:08:41

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))


mark.warren
2018-11-20 09:09:08

(by-2 3)


mark.warren
2018-11-20 09:10:00

Is this the right way of doing partial application in Racket or is there another way?


soegaard2
2018-11-20 09:11:24

Yes. It’s the right way. An anternative: (define ((multiply-by y) x) (* x y)) (hope I got the syntax right)


soegaard2
2018-11-20 09:11:48

I prefer your way though.


mark.warren
2018-11-20 09:12:59

Cool, thanks, glad I’m on the right lines.


jerome.martin.dev
2018-11-20 09:13:06

Once you get the hang of it, you can use curry: (define by-2 (curry * 2)) (by-2 3)


mark.warren
2018-11-20 09:13:38

I was going to try and get my head round currying next.


jerome.martin.dev
2018-11-20 09:14:16

it does pretty much what you do in the background :slightly_smiling_face:


mark.warren
2018-11-20 09:14:52

Looks that way, very neat.


jerome.martin.dev
2018-11-20 09:20:46

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)


jerome.martin.dev
2018-11-20 09:22:32

The only drawback is that cut is a syntax while curry is a procedure


mark.warren
2018-11-20 09:23:49

Wow, that’s blown my mind.


mark.warren
2018-11-20 09:24:29

Sort of placeholder parameters?


jerome.martin.dev
2018-11-20 09:24:34

yep


mark.warren
2018-11-20 09:24:45

Neat


jerome.martin.dev
2018-11-20 09:25:06

There’s usually other ways to do it, but sometimes it’s handy


mark.warren
2018-11-20 09:26:27

Yes, there does always seem to be many ways of doing anything :grinning:


jerome.martin.dev
2018-11-20 09:29:48

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.


jerome.martin.dev
2018-11-20 09:31:16

I’d say cut is doomed to disappear quickly anywhere performance is at stake.


jerome.martin.dev
2018-11-20 09:34:30

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))


jerome.martin.dev
2018-11-20 09:35:59

The really handy part about cut is that is allows to easily use a library in which parameters are not in a practical order


jerome.martin.dev
2018-11-20 09:36:18

for “glue code” it’s perfect


mark.warren
2018-11-20 09:53:49

@jerome.martin.dev Thanks for that.


samth
2018-11-20 14:40:28

@jerome.martin.dev personally, rather than cut I like fancy-app package (of course, I did write it): https://github.com/samth/fancy-app/


mark.warren
2018-11-20 14:49:13

@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.


jerome.martin.dev
2018-11-20 14:49:16

@samth It’s 46 lines of goodness :smile:


samth
2018-11-20 14:49:35

it’s inspired by scala (the clojure one is a bit different)


mark.warren
2018-11-20 14:50:06

Of course, I didn’t read the top line.


greg
2018-11-20 15:44:57

threading is one inspired more by Clojure


greg
2018-11-20 15:47:06

“Functional programming” is a paradigm where people explore all possible ways to not-supply parameters to pure functions. :stuck_out_tongue:


mark.warren
2018-11-20 15:49:55

Hehe


lexi.lambda
2018-11-20 16:25:20

curly-fn also exists, and it provides a Clojure-style function shorthand.


lexi.lambda
2018-11-20 16:25:28

(It’s my package.)


mark.warren
2018-11-21 07:24:53

Ah, so many choices.