Clojure differences between Ref, Var, Agent, Atom, with examples

I highly recommend “The Joy of Clojure” or “programming Clojure” for a real answer to this question, I can reproduce a short snip-it of the motivations for each: start by watching this video on the notion of Identity and/or studying here. Refs are for Coordinated Synchronous access to “Many Identities”. Atoms are for Uncoordinated synchronous … Read more

Clojure: reduce vs. apply

reduce and apply are of course only equivalent (in terms of the ultimate result returned) for associative functions which need to see all their arguments in the variable-arity case. When they are result-wise equivalent, I’d say that apply is always perfectly idiomatic, while reduce is equivalent — and might shave off a fraction of a … Read more

A regex to match a substring that isn’t followed by a certain other substring

Try: /(?!.*bar)(?=.*foo)^(\w+)$/ Tests: blahfooblah # pass blahfooblahbarfail # fail somethingfoo # pass shouldbarfooshouldfail # fail barfoofail # fail Regular expression explanation NODE EXPLANATION ——————————————————————————– (?! look ahead to see if there is not: ——————————————————————————– .* any character except \n (0 or more times (matching the most amount possible)) ——————————————————————————– bar ‘bar’ ——————————————————————————– ) end of … Read more

Why does Clojure have “keywords” in addition to “symbols”?

Here’s the Clojure documentation for Keywords and Symbols. Keywords are symbolic identifiers that evaluate to themselves. They provide very fast equality tests… Symbols are identifiers that are normally used to refer to something else. They can be used in program forms to refer to function parameters, let bindings, class names and global vars… Keywords are … Read more

How to create default value for function argument in Clojure

A function can have multiple signatures if the signatures differ in arity. You can use that to supply default values. (defn string->integer ([s] (string->integer s 10)) ([s base] (Integer/parseInt s base))) Note that assuming false and nil are both considered non-values, (if (nil? base) 10 base) could be shortened to (if base base 10), or … Read more

What are the differences between Clojure, Scheme/Racket and Common Lisp?

They all have a lot in common: Dynamic languages Strongly typed Compiled Lisp-style syntax, i.e. code is written as a Lisp data structures (forms) with the most common pattern being function calls like: (function-name arg1 arg2) Powerful macro systems that allow you to treat code as data and generate arbitrary code at runtime (often used … Read more

Why exactly is eval evil?

There are several reasons why one should not use EVAL. The main reason for beginners is: you don’t need it. Example (assuming Common Lisp): EVALuate an expression with different operators: (let ((ops ‘(+ *))) (dolist (op ops) (print (eval (list op 1 2 3))))) That’s better written as: (let ((ops ‘(+ *))) (dolist (op ops) … Read more