Search code examples
clojure

Using partials within function as value on a map


Firstly; sorry if the terminology I'm using is incorrect, I'm still very new to clojure and the paradigm shift is taking some time.

I am trying to work with a function which takes the first item from a set which is greater than twelve (is a 'teen' number). I can write this when I'm just applying it directly to a set, but I'm unsure how to write the function within a map. Can anyone point me in the right direction?

I tried a few things, typically along the lines of (partial (first (filter (partial < 12)))) but without any luck at all so far, and researching definitions of filter/partial has not yet proved fruitful.

TL/DR I want to have, as a value in a map, a function which takes the first item in a list which is greater than 12.

(def test-set [1, 8, 15, 22, 29])

(def some-functions {
  :first first
  :last last
  :teenth "I don't know what to put here"
})


(first (filter (partial < 12) test-set))

Solution

  • One way is to use an anonymous function when defining the map (https://riptutorial.com/clojure/example/15544/defining-anonymous-functions)

    > (def some-functions {
      :first first
      :last last
      :teenth #(first (filter (partial < 12) %))})
    
    > ((:first some-functions) test-set)
    1
    
    > ((:last some-functions) test-set)
    29
    
    > ((:teenth some-functions) test-set)
    15
    

    Of course you could also have explicitly defined your function and used it in your map:

    > (defn teenth [coll] (first (filter (partial < 12) coll)))
    
    > (def some-functions {
      :first first
      :last last
      :teenth teenth})
    

    (As an aside, be careful with the word set. In clojure sets are unordered collections of unique values. https://clojure.org/reference/data_structures)