Code Monkey home page Code Monkey logo

Comments (4)

kanaka avatar kanaka commented on May 13, 2024

Can you give a code example of what you are trying to do?

from mal.

ktodyruik avatar ktodyruik commented on May 13, 2024

Yup.

For example,

(def! people '({"Name" "Homer" "Age" "43"} {"Name" "Bart" "Age" "8"}))

; This is easy and works. I would like to try rewriting it using ->
(map (fn* (p) (get p "Name")) people)
; This doesn't work as I don't think the map will return a partially applied function. Some languages do this automatically.
(-> people
      (map (fn* (p) (get p "Name")))

; I could write a new function
(def! map-people (fn* (people) (map (fn* (p) (get p "Name")) people)))

; Then apply it like this and it works
(-> people map-people)

; I could try an anonymous function. But there must be an error here as I can't get this to work.
(-> people (fn* (ppl) (map (fn* (p) (get p "Name")) ppl)))

Is there a better way of rewriting my original map in the first example using ->?

from mal.

wasamasa avatar wasamasa commented on May 13, 2024

Threading macros and lambdas don't mix terribly well. In the case of map, it's common to see ->> for cleaning up an unwieldy expression the anonymous function is applied to. One can also mix both when needed.

from mal.

ktodyruik avatar ktodyruik commented on May 13, 2024

@wasamasa Thanks. I think I was misunderstanding the difference between thread first and last and confusing what was going wrong with my expression. I thought it had something to do with partial function application. I see now that I was just using the wrong threading macro.

Here is my working code:

(def! people '({"Name" "Homer" "Age" "43"} {"Name" "Bart" "Age" "8"}))

; This is easy and works. I would like to try rewriting it using ->>
(map (fn* (p) (get p "Name")) people)

; Here it is rewritten (works now)
(->> people 
     (map (fn* (p) (get p "Name"))))

; I wanted to chain filter and map together so I did that next
(->> people 
     (filter (fn* (p) (eq? (get p "Age") "43")))
     (map (fn* (p) (get p "Name"))))     

Thanks for your help.
Kerry

from mal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.