Skip to content

Instantly share code, notes, and snippets.

@adomokos
adomokos / type_vs_data_constructors.md
Created April 5, 2017 02:18
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
adomokos / Makefile
Last active February 18, 2020 00:21
Makefile template
DBUSER=db_user
DBPASSWD=db_password
DBNAME=some_db
THIS_FILE := $(lastword $(MAKEFILE_LIST))
.DEFAULT_GOAL := help
build-db: ## Builds the DB
dropdb --if-exists --username $(DBUSER) $(DBNAME)
@adomokos
adomokos / keybase.md
Created April 9, 2019 20:22
Keybase Proof

Keybase proof

I hereby claim:

  • I am adomokos on github.
  • I am adomokos (https://keybase.io/adomokos) on keybase.
  • I have a public key ASA8JcfM-KoBRcr5g7J-bADE-azTB3JtbjP0B6yn84UXygo

To claim this, I am signing this object:

@adomokos
adomokos / visitor_pattern_example.rb
Created May 24, 2011 17:28
The Visitor Pattern implementation in Ruby from the Wikipedia example
class CarElement
def accept(visitor)
raise NotImpelementedError.new
end
end
module Visitable
def accept(visitor)
visitor.visit(self)
end
@adomokos
adomokos / get_out_of_my_controller_spec.rb
Created September 7, 2011 19:38
The supporting code for my blog entry: "Get out of my Controller! And from Active Record, too!"
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
end
class Discussion < ActiveRecord::Base
# These fields are defined dynamically by ActiveRecord
@adomokos
adomokos / RomanNumeralsSpec.hs
Created April 26, 2017 04:42
The Roman Numerals kata in Haskell
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"),
@adomokos
adomokos / string_calculator_test.go
Created January 5, 2017 03:42
String Calculator in Go
package string_calculator
import (
"github.com/stretchr/testify/assert"
"strconv"
"strings"
"testing"
)
func Convert(params ...string) int {
@adomokos
adomokos / string_calculator.hs
Created July 14, 2016 17:44
String Calculator in Haskell
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
(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
adomokos / keyword_like_function_test.clj
Created January 6, 2014 14:40
An example to create keyword-like arguments for a Clojure function
(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]))))