partial

added
1.0

ns
clojure.core

type
function

(partial f) (partial f arg1) (partial f arg1 arg2) (partial f arg1 arg2 arg3) (partial f arg1 arg2 arg3 & more)

Takes a function f and fewer than the normal arguments to f, and
returns a fn that takes a variable number of additional args. When
called, the returned function calls f with args + additional args.

                user=> (def to-english (partial clojure.pprint/cl-format nil "~@(~@[~R~]~^ ~A.~)"))
#'user/to-english

user=> (to-english 1234567890)
"One billion, two hundred thirty-four million, five hundred sixty-seven thousand, eight hundred ninety"

            
                user=> (def hundred-times (partial * 100))
#'user/hundred-times

user=> (hundred-times 5)
500

user=> (hundred-times 4 5 6)
12000

user=> (def add-hundred (partial + 100))
#'user/add-hundred

user=> (add-hundred 5)
105

            
                (def subtract-from-hundred (partial - 100))

user=> (subtract-from-hundred 10)      ; same as (- 100 10)
90

user=> (subtract-from-hundred 10 20)   ; same as (- 100 10 20)
70
            
                ; Maps exponent to coefficient
; x^3 + 2x + 1
(def poly (fn [n]
  (cond
    (= 0 n) 1
    (= 1 n) 2
    (= 3 n) 1
    :else 0)
  )
)

; Differentiates input by returning a polynomial that is curried
; 3x^2 + 2
(defn diff [p]
  (partial (fn [p n] (* (+ 1 n) (p (+ 1 n)))) p)
)

(poly 3)
;=> 1
((diff poly) 3)
;=> 0
((diff poly) 2)
;=> 3

            
                user=> (defn fun-full [x y] (+ x y))
;=> #<function user$fun_full(x,y){ ...

user=> (fun-full 2 3)
;=> 5

user=> (def fun-half (partial fun-full 2))
;=> #<function (x,y,z,var_args){ ...

user=> (fun-half 3)
;=> 5

            
                ;;Takes a function f and the normal full arguments is allowed

user=> (defn add [x y] (+ x y))
#'user/add
user=> (partial add 1 1 )
#object[clojure.core$partial$fn__4529 0x5eb8fe04 "clojure.core$partial$fn__4529@5eb8fe04"]
user=> (apply (partial add 1 1 ) nil)
2
user=> ((partial add 1 1 ))
2
user=> ((partial add 1 1 1))
ArityException Wrong number of args (3) passed to: user/add  clojure.lang.AFn.throwArity (AFn.java:429)

user=>
            
                user=> (def add1 (partial + 1))
#'user/add1
user=> (add1)
;=> 1
user=> (add1 2)
;=> 3
user=> (add1 2 3 4)
;=> 10
user=> (= (add1 2 3 4) (+ 1 2 3 4))
;=> true
            
                (def times (partial *))

(times 1) ; -> 1

(times 1 2 3) ; -> 6

(* 1 2 3) ; -> 6


(def add-hundred (partial + 100))

(add-hundred 1) ; -> 101

(add-hundred 1 2 3) ; -> 106

(+ 100 1 2 3) ; -> 106
            
                ;; Check if a character is vowel

(def vowel? #(some (partial = %) "aiueo"))

(vowel? \\e)
;;=> true

(vowel? \\c)
;;=> nil
            
                ;; apply feeds sequence items as variable args to the conj function
;; variable args gets converted to list in the function arg and hence conj 
;; adds them as a list
(apply #(conj [0 1] %&) [2 3 4 5])
;;=> [0 1 (2 3 4 5)]

;; Partial offers are mechanism to feed the variable args as is to the conj 
;; function effectively  like (conj [] 2 3 4 5)
(apply (partial conj [0 1]) [2 3 4 5])
;;=> [0 1 2 3 4 5]