Skip to content

Instantly share code, notes, and snippets.

View TheSeamau5's full-sized avatar

Hassan Hayat TheSeamau5

  • Entrepreneur
  • Austin, TX
View GitHub Profile
@TheSeamau5
TheSeamau5 / example.elm
Last active August 29, 2015 14:16
API for testing stateful code in Elm
{-
This is an example of how to test state in Elm.
The idea is that one can model your application as a state machine
with an update function of the type:
update : action -> state -> state
This is very useful in order to test applications
built using the Elm architecture, which looks like this:
@TheSeamau5
TheSeamau5 / hoveringGridExample.elm
Created March 6, 2015 01:49
Example of hovering over a grid
import Graphics.Collage (..)
import Graphics.Element (..)
import Graphics.Input (..)
import Color (..)
import Signal (..)
import Text
import List
import Mouse
@TheSeamau5
TheSeamau5 / svgdragdrop.elm
Last active January 29, 2020 05:26
Drag and drop example with svg in Elm
import Svg (Svg, circle, svg, g, line, text)
import Svg.Attributes (cx, cy, r, fill, stroke, strokeWidth, x, y, x1, x2, y1, y2, fontSize, style)
import Html
import Html.Attributes as Html
import Signal (Signal, map, foldp)
import DragAndDrop (mouseEvents, MouseEvent(..))
import List
--------------
@TheSeamau5
TheSeamau5 / csslayout.elm
Created January 31, 2015 17:48
Wrapping CSS Layout in Elm
type alias Bounds = {
left : Float,
right : Float,
top : Float,
bottom : Float
}
bounds : Bounds
bounds = Bounds 0 0 0 0
@TheSeamau5
TheSeamau5 / CSP.md
Created January 31, 2015 02:35
Investigating CSP for Elm

Communicating Sequential Processes (CSP)

CSP is one of the many ways of reasoning about concurrent systems. It emphasizes on the idea of a channel which enables communication between producers and consumers and where the channel itself is fundamentally decouple from either producers or consumers.

Queues

The easiest way of thinking about a channel is to think about a queue. A queue is very simple, it is a data structure where one may only push data to the end of a queue and may only read data from the front of the queue.

In pseudo-code:

@TheSeamau5
TheSeamau5 / interface.elm
Last active August 29, 2015 14:14
Opinion on how typeclasses would look in Elm
interface Numerical a = {
(+) : a -> a -> a,
(-) : a -> a -> a,
(*) : a -> a -> a,
negate : a -> a,
abs : a -> a
}
implementation Numerical Float = {
(+) = Native.Float.add,
@TheSeamau5
TheSeamau5 / mapAllArray.elm
Created January 25, 2015 19:00
mapAll for Arrays
mapAll : (a -> a -> a) -> Array a -> Array (Array a)
mapAll f array =
let innerMap tempArray =
case head tempArray of
Nothing -> empty
Just x -> push (map (f x) array) (innerMap (rest tempArray))
in
innerMap array
@TheSeamau5
TheSeamau5 / QuadTree.elm
Created January 25, 2015 16:28
Quadtrees in Elm
import Array (..)
import Text (asText)
import Graphics.Collage (..)
import Color (..)
drawBox box =
move (box.horizontal.low + boxHalfWidth box, box.vertical.low + boxHalfHeight box) <|
outlined (solid black) <|
rect (boxWidth box) (boxHeight box)
@TheSeamau5
TheSeamau5 / Loops.elm
Created January 24, 2015 23:43
A neat construct to loop in Elm without worrying about stack problems
loop : a -> (a -> Bool) -> (a -> a) -> (a -> b) -> b
loop start condition update return =
trampoline <|
loop' start condition update return
loop' : a -> (a -> Bool) -> (a -> a) -> (a -> b) -> Trampoline b
loop' start condition update return =
case condition start of
True -> Done (return start)
@TheSeamau5
TheSeamau5 / shuffleList.elm
Created January 18, 2015 02:11
Function to shuffle a list (useful to shuffle cards for example)
import List (..)
import Random (..)
import Text (asText)
shuffle : List a -> List a
shuffle lt =
let len = length lt
fgen = float 0 1
lgen = list len fgen
rlist = fst <| generate lgen (initialSeed 31415)