Skip to content

Instantly share code, notes, and snippets.

@finalfantasia
finalfantasia / understanding_transducer_composition_in_clojure.clj
Last active November 25, 2017 21:12
Understanding Transducer Composition in Clojure
;; this is a little Clojure code snippet that hopefully helps one understand why
;; the order in which components of a composite transducer passed to the `comp`
;; fn feels backward when compared to that of regular fn compositions.
;; in the following `defn`s, `rf` is a "reducing fn", i.e., any fn that `reduce`
;; expects as its first argument (e.g., `+`, `conj`, etc.)
;; a transducer
(defn g [rf]
(fn [r x] ; this fn itself is a reducing fn
@finalfantasia
finalfantasia / forms_and_expressions_in_clojure.md
Last active June 3, 2017 01:41
Forms and Expressions in Clojure

Clojure programs are composed of expressions. Every form not handled specially by a special form or macro is considered by the compiler to be an expression, which is evaluated to yield a value. [1]

An expression is a form which will be (or can be) evaluated in the final program. [2]

For instance, consider the form (fn [x] (inc x)). It is a list of three elements: the special form fn, the vector [x], and the list (inc x). All of those elements are forms, but only the last is an expression, because it is "intended" for evaluation; the first two forms are shuffled around by the macroexpander but never evaluated. The outermost form (fn [x] (inc x)) is itself an expression as well.

This seems to be an interesting distinction, but it does mean it is context-sensitive: [x] is always a form, and may or may not be an expression depending on context. For instance, as in the example above ((fn [x] (inc x))), [x] is a form not an expression because it's handled specially by the special form fn;

@finalfantasia
finalfantasia / cons_and_lazy_sequences_in_clojure.md
Last active November 7, 2022 16:54
Cons and Lazy Sequences in Clojure

(conj collection item) adds item to collection. To do that, it needs to realize collection. (I'll explain why below.) So the recursive call happens immediately, rather than being deferred.

(cons item collection) creates a sequence which begins with item, followed by everything in collection. Significantly, it doesn't need to realize collection. So the recursive call will be deferred (because of using lazy-seq) until somebody tries to get the tail of the resulting sequence.

The following is how it works internally:

cons actually returns a clojure.lang.Cons object, which is what lazy sequences are made of. conj returns the same type of collection which you pass it (whether that is a list, vector, or whatever else). conj does this using a polymorphic Java method call on the collection itself. (See line 524 of clojure/src/jvm/clojure/lang/RT.java.)

What happens w

@finalfantasia
finalfantasia / solutions_to_4clojure_problems.clj
Last active May 21, 2021 05:50
Solutions to 4Clojure Problems
;;
;; www.4clojure.com
;;
;; #19 last element
(partial reduce (fn [_ x] x))
;; (fn [coll] (reduce (fn [_ x] x) coll))
;; (partial reduce #(identity %2))
;; (partial reduce #(second %&))
@finalfantasia
finalfantasia / make_arabic_letters_connected_in_emacs.el
Last active January 10, 2017 08:03
Make Arabic Letters Connected in Emacs
;; Evaluate the following to get a list of all typefaces and pick an Arabic typeface from it.
;; (In my case, it was "PakType Naskh Basic".):
(print (font-family-list))
;; Add the following line to ~/.emacs/init.el (or ~/.emacs)
;; (Change the name of the typeface at the end to that of the one you picked above.):
(when window-system
(set-fontset-font "fontset-default" '(#x600 . #x6ff) "PakType Naskh Basic"))
@finalfantasia
finalfantasia / react_redux_todo_app.js
Last active November 10, 2016 06:56
React-Redux Todo App
// <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react.js"></script>
// <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.js"></script>
/*
<body>
<div id="root" />
</body>
*/
// redux api
@finalfantasia
finalfantasia / todo_list_redux_reducer.js
Last active February 21, 2022 03:30
Todo List Redux Reducer
// <script src="https://cdnjs.cloudflare.com/ajax/libs/expect/1.20.2/expect.min.js"></script>
// redux api
const combineReducers = reducers =>
(state = {}, action) =>
Object.keys (reducers).
reduce ((accumulator, key) => Object.assign ({}, accumulator, {[key]: reducers [key] (state [key], action)}), {})
@finalfantasia
finalfantasia / java_generics.md
Last active May 8, 2021 16:32
Java Generics

Terms

  • Generic Type
  • Generic Method
  • Type Parameter
  • Type Argument
  • Parameterized Type
  • Type Erasure
  • Wildcard (?)

Example

@finalfantasia
finalfantasia / why_functional_programming_has_not_taken_over_yet.md
Created March 3, 2016 18:44
Why functional programming hasn't taken over yet

Because all those advantages are also disadvantages.

Stateless programs; No side effects

Real-world programs are all about side effects and mutation. When the user presses a button it's because they want something to happen. When they type in something, they want that state to replace whatever state used to be there. When Jane Smith in accounting gets married and changes her name to Jane Jones, the database backing the business process that prints her paycheque had better be all about handling that sort of mutation. When you fire the machine gun at the alien, most people do not mentally model that as the construction of a new alien with fewer hit points; they model that as a mutation of an existing alien's properties. When the programming language concepts fundamentally work against the domain being modelled, it's hard to justify using that language.

Concurrency; Plays extremely nice with the rising multi-core technology

The problem is just pushed around. With immutable data structures you have ch

@finalfantasia
finalfantasia / clean_and_stop_creation_of_wdmc_directories_on_wd_my_cloud_ex2.md
Last active September 14, 2015 07:55
Clean and Stop Creation of .wdmc Directories on WD My Cloud EX2
  1. Stop the daemons:
    /etc/init.d/wdmcserverd stop
    /etc/init.d/wdphotodbmergerd stop
  1. Disable the daemons:
    update-rc.d wdphotodbmergerd disable
    update-rc.d wdmcserverd disable