Skip to content

Instantly share code, notes, and snippets.

addColor :: forall (c :: Symbol) t. KnownSymbol c => SomeColor -> ThemeInstance t -> ThemeInstance (c : t)
addColor sc (ThemeInstance m) = ThemeInstance $ Map.insert colorName sc m
where colorName = symbolVal (Proxy @c)
emptyThemeInstance :: ThemeInstance '[]
emptyThemeInstance = ThemeInstance Map.empty
@paraseba
paraseba / performance.hs
Created June 20, 2018 16:32
Uncommenting the type signature produces slower code
import qualified Data.Vector.Generic.Mutable as MV
--mutableEval :: MV.MVector v Int8 => [Op] -> v RealWorld Int8 -> Int -> IO Int
mutableEval [] _ pos = return pos
mutableEval (op:ops) mem pos = case op of
Inc n memOffset ->
(MV.unsafeModify mem (+ fromIntegral n) (pos + coerce memOffset) >> mutableEval ops mem pos)
MRight n -> mutableEval ops mem (pos + coerce n)
@paraseba
paraseba / scopedLens.hs
Last active June 3, 2018 23:21
How to refer to a type variable hidden under an alias?
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ScopedTypeVariables #-}
type Lens s t a b =
forall f.
Functor f =>
(a -> f b)
-> s
-> f t
@paraseba
paraseba / folds.scala
Created May 30, 2018 14:04
Demonstration of implementing foldRight with by-name parameter
/*
This is a demonstration of foldLeft and foldRight in scala using
by-name parameters to process infinite streams and to short-circuit
computations that can finish early.
*/
import scala.annotation.tailrec
/*
foldLeft can be written in tail recursive form. The recursive call is not
[latexmath]
++++
f(p) \leq q \iff p \leq g(q)
++++
(defn f1 [n]
(lazy-seq
(cons n (f1 (inc n)))))
(nth (f1 0) 5000)
;; => 5000
(defn f2 [n]
(lazy-seq
(cons n (map identity (f2 (inc n))))))
; Currencies know how to create Money
(defprotocol Currency
(make-money [this amount]))
; Money knows the details about how to print itself, round, etc.
; We want to be able to pass Moneys around, and after the fact, be able to
; print them, round them, etc.
(defprotocol Money
(str$ [this])
(round$ [this]))
@paraseba
paraseba / gist:1328334
Created October 31, 2011 18:21
Setup your environment for the Clojure hands-on training

##Install Java

Install any version of Java JDK >= 1.5 You can download Java using your system's package manager or going to http://www.oracle.com/technetwork/java/javase/downloads/index.html

##Install Leiningen

Leiningen will help us automating some common tasks like installing dependencies and running the REPL. The installation procedure changes if you are in a Unix like system or in Windows. If you have any problems, there is more information in Leiningen's page: https://github.com/technomancy/leiningen

(def file-path "words.txt")
@paraseba
paraseba / gist:1167254
Created August 24, 2011 03:40
Jorge Luis Borges on novice writers (or programmers?)
Look, I mean to say this: When I began writing, I thought that
everything should be defined by the writer. For example, to say
“the moon” was strictly forbidden; that one had to find an
adjective, an epithet for the moon. (Of course, I'm simplifying
things. I know it because many times I have written “la luna,”
but this is a kind of symbol of what I was doing.) Well, I
thought everything had to be defined and that no common turns
of phrase should be used. I would never have said, “So-and-so
came in and sat down,” because that was far too simple and far
too easy. I thought I had to find out some fancy way of saying