Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assoc? Adding a few more functional operators #108

Open
Inc0n opened this issue Apr 6, 2023 · 1 comment
Open

assoc? Adding a few more functional operators #108

Inc0n opened this issue Apr 6, 2023 · 1 comment

Comments

@Inc0n
Copy link

Inc0n commented Apr 6, 2023

Assoc and assoc-in are analogous to put and put-in without the side effects.

There is first which is sometimes more convenient than (map (fn [x] (x 0)) xs). However, there isn't a second so we have (map first xs) and (map (fn [x] (x 1)) xs)`.

There may be more of them that could be useful for FP that I haven't thought of ATM.

Would it be a good idea to add them as part of spork?

@Inc0n Inc0n changed the title conj? assoc? Adding a few more functional operators assoc? Adding a few more functional operators Apr 6, 2023
@eko234
Copy link

eko234 commented Sep 10, 2024

Hello, rn I'm indeed playing with that idea, I don't know if its the best way to do it, but by design it is not going to be performant anyway :V

(defn assoc [ds k v & ekvs]
  (cond
   (struct? ds) (struct ;(kvs ds) k v ;ekvs)
   (and (tuple? ds) (empty? ekvs) (number? k))
   (if (= k (length ds))
     (tuple ;ds v)
     (tuple ;(slice ds 0 k) v ;(slice ds (inc k) (length ds))))
   (error "ds must be a struct or a tuple and tuples' keys must be numbers, also tuples do'nt accept extra kv's")))

(defn assoc-in [ds [k & ks] v]
  (if (empty? ks)
    (assoc ds k v)
    (assoc ds k (assoc-in (get ds k {}) ks v))))

AFIK it works as in clojure, it is something trivial enough so people can add it to their projects by themselves, but I think it would be nice to have it at hand with spork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants