Skip to content

Instantly share code, notes, and snippets.

adomokos /
Created Apr 5, 2017
Type vs Data Constructors in Haskell

Type vs Data Constructors

In a data declaration, a type constructor is the thing on the left hand side of the equals sign. The data constructor(s) are the things on the right hand side of the equals sign. You use type constructors where a type is expected, and you use data constructors where a value is expected.

Data constructors

To make things simple, we can start with an example of a type that represents a colour.

data Colour = Red | Green | Blue
adomokos / Makefile
Last active Feb 18, 2020
Makefile template
View Makefile
THIS_FILE := $(lastword $(MAKEFILE_LIST))
build-db: ## Builds the DB
dropdb --if-exists --username $(DBUSER) $(DBNAME)

Keybase proof

I hereby claim:

  • I am adomokos on github.
  • I am adomokos ( on keybase.
  • I have a public key ASA8JcfM-KoBRcr5g7J-bADE-azTB3JtbjP0B6yn84UXygo

To claim this, I am signing this object:

adomokos / visitor_pattern_example.rb
Created May 24, 2011
The Visitor Pattern implementation in Ruby from the Wikipedia example
View visitor_pattern_example.rb
class CarElement
def accept(visitor)
module Visitable
def accept(visitor)
adomokos / get_out_of_my_controller_spec.rb
Created Sep 7, 2011
The supporting code for my blog entry: "Get out of my Controller! And from Active Record, too!"
View get_out_of_my_controller_spec.rb
module ActiveRecord; class Base; end; end
# The AR Models Rails give you
class User < ActiveRecord::Base
# These fields are defined dynamically by ActiveRecord
attr_accessor :id, :full_name
class Discussion < ActiveRecord::Base
# These fields are defined dynamically by ActiveRecord
adomokos / RomanNumeralsSpec.hs
Created Apr 26, 2017
The Roman Numerals kata in Haskell
View RomanNumeralsSpec.hs
import Test.Hspec
import Test.QuickCheck
import Control.Exception (evaluate)
import qualified Data.Map as Map
import Data.List
mapping :: Map.Map Int String
mapping = Map.fromList [
(1, "I"),
(4, "IV"),
View string_calculator_test.go
package string_calculator
import (
func Convert(params ...string) int {
adomokos / string_calculator.hs
Created Jul 14, 2016
String Calculator in Haskell
View string_calculator.hs
module StringCalculator where
import Test.Hspec
import Test.QuickCheck
import Control.Exception (evaluate)
import Data.List.Split (splitOn)
calculator' :: String -> Char -> Integer
calculator' x c = sum $ coerceString x c
View change_maker.clj
(ns change-maker.core-test
(:require [clojure.test :refer :all]))
(def ^:dynamic *denominations* [25 10 5 1])
(defn best-match [value]
(first (filter #(<= % value) *denominations*)))
(defn calculate-change [value product]
(let [reducer (best-match value)]
adomokos / keyword_like_function_test.clj
Created Jan 6, 2014
An example to create keyword-like arguments for a Clojure function
View keyword_like_function_test.clj
(ns keyword-like-function-test
(:require [clojure.test :refer :all]))
(defn find-values
[& {:keys [p1 p2] :or {p1 [0 0] p2 [1 1]}}]
[p1 p2])
(deftest keyword-arguments-test
(testing "when both of the arguments were passed"
(is (= [4 15] (first (find-values :p1 [4 15] :p2 [3 21]))))