This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inspired by: | |
# http://learnyouahaskell.com/a-fistful-of-monads#walk-the-line | |
# Put simply, pierre is a tight rope walker. birds land on the left | |
# or right of his pole. If the difference is > 3 he falls off | |
ExUnit.start | |
defmodule WalkTheLine do | |
use ExUnit.Case |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type BasketItem struct { | |
Id string | |
Price int | |
} | |
type Basket struct { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
var jsonStr = []byte(` | |
{ | |
"things": [ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
//Setup some types for functions we want to pass around | |
type unaryFloat func(float64) float64 | |
type binaryFloat func(float64, float64) float64 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
a := 0 | |
b := 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn collatz [n] | |
(if (even? n) | |
(/ n 2) | |
(inc (* 3 n)))) | |
(defn collatz-seq [start ls] | |
(let [next-number (collatz start)] | |
(if (= start 1) | |
(conj ls 1) | |
(collatz-seq next-number (conj ls start))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn under [verb, obverse, func x y] | |
(obverse (func (verb x) (verb y)))) | |
(defn sqrt [x] | |
(Math/sqrt x)) | |
(defn square [x] | |
(Math/pow x 2)) | |
(defn pythagoras [x y] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;Will Suffer from rounding errors - but ok for an example | |
(defn under [verb, obverse, func x y] | |
(obverse (func (verb x) (verb y)))) | |
(defn log [x] | |
(Math/log x)) | |
(defn exp [x] | |
(Math/exp x)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
zeroes 0 = [] | |
zeroes n = [0] ++ (zeroes (n-1)) | |
foo x y z= zipWith (+) (zipWith (+) x y) z | |
grid_neighbours l = | |
let top = map inc_neighbours ( [(zeroes (length (head l)))] ++ (take (length l) l) ) | |
bottom = map inc_neighbours ((drop 1 l) ++ [(zeroes (length (head l)))]) | |
current = map neighbours l | |
in zipWith3 foo top bottom current |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; mattyw's solution to Reverse a Sequence | |
;; https://4clojure.com/problem/23 | |
(defn reverse-func [seq] | |
(reduce (fn [a b] (cons b a)) '() seq)) | |
(println (= (reverse-func [1 2 3 4 5]) [5 4 3 2 1])) |