-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair.cljc
35 lines (27 loc) · 1.27 KB
/
pair.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(ns monnit.pair
"A two-element tuple, smaller than a vector. Implements [[monnit.core/Functor]]."
(:require [monnit.core :as m]))
(declare ->Pair)
(defrecord
^{:doc "A two-element tuple, smaller than a vector. Implements [[monnit.core/Functor]]."}
Pair [fst snd]
m/Functor
(-fmap [_ f] (->Pair fst (f snd)))
(-fmap [_ f b]
(->Pair (m/sconcat fst (.-fst ^Pair b))
(f snd (.-snd ^Pair b))))
(-fmap [_ f b c]
(->Pair (m/sconcat fst (.-fst ^Pair b) (.-fst ^Pair c))
(f snd (.-snd ^Pair b) (.-snd ^Pair c))))
(-fmap [_ f b c d]
(->Pair (m/sconcat fst (.-fst ^Pair b) (.-fst ^Pair c) (.-fst ^Pair d))
(f snd (.-snd ^Pair b) (.-snd ^Pair c) (.-snd ^Pair d))))
(-fmap [_ f b c d args]
(->Pair (apply m/sconcat fst (.-fst ^Pair b) (.-fst ^Pair c) (.-fst ^Pair d)
(map (fn [^Pair arg] (.-fst arg)) args))
(apply f snd (.-snd ^Pair b) (.-snd ^Pair c) (.-snd ^Pair d)
(map (fn [^Pair arg] (.-snd arg)) args)))))
(def ^{:arglists '([fst snd])} pair "Create a [[Pair]]" ->Pair)
(defn pair? "Is `p` a pair?" [p] (instance? Pair p))
(defn fst "Get the first element of the [[Pair]] `p`" [^Pair p] (.-fst p))
(defn snd "Get the second element of the [[Pair]] `p`" [^Pair p] (.-snd p))