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 / metaAnnotations.elm
Last active August 29, 2015 14:12
Proposal for meta annotations in Elm
-- REQUIRE META ANNOTATION:
-- The require meta-annotation will force the input record type vector to have a z field
require {z : Float} from vector
moveZ : Float -> vector -> vector
moveZ z v = { v | z <- v.z + z }
-- The top code is equivalent to
moveZ : Float -> { a | z : Float } -> { a | z : Float }
moveZ z v = { v | z <- v.z + z }
@TheSeamau5
TheSeamau5 / VisualBinaryTree.elm
Last active September 19, 2015 19:05
Visual representation of binary trees in Elm
import Text (asText)
import Graphics.Collage (collage, toForm, filled, circle, move, scale, Form)
import Graphics.Element (Element)
import Color (red)
import List (map, (::), reverse)
type BinaryTree a = Nil | Node (BinaryTree a) a (BinaryTree a)
insert : comparable -> BinaryTree comparable -> BinaryTree comparable
insert node tree =
@TheSeamau5
TheSeamau5 / BinaryTree.elm
Created January 5, 2015 15:15
Binary Tree in Elm
type BinaryTree a = Nil | Node (BinaryTree a) a (BinaryTree a)
insert : comparable -> BinaryTree comparable -> BinaryTree comparable
insert node tree =
case tree of
Nil -> Node Nil node Nil
Node left leaf right ->
if | leaf == node -> Node left leaf right
| leaf < node -> Node left leaf (insert node right)
| leaf > node -> Node (insert node left) leaf right
@TheSeamau5
TheSeamau5 / gridMapWithIndices.elm
Last active August 29, 2015 14:12
Function to map a function over a grid while passing in the indices
gridMapWithIndices : (Int -> Int -> a -> b) -> List (List a) -> List (List b)
gridMapWithIndices function grid =
let iterateColumns x y columnCount row =
if (x >= columnCount || row == []) then []
else
function x y (head row) :: iterateColumns (x + 1) y columnCount (tail row)
iterateRows y rowCount grd =
if (y >= rowCount) then []
else
@TheSeamau5
TheSeamau5 / QuasiFullECS.elm
Last active August 3, 2017 23:09
Quasi-Full expression of Entity Component System in Elm
import Color (Color, rgb)
import Graphics.Collage (..)
import Graphics.Element (Element)
import List ((::), map)
import Signal (Signal, foldp, (<~), sampleOn)
import Keyboard (arrows)
import Time (millisecond, every)
--------------------------
type alias Entity = {
position : Vector,
velocity : Vector,
scale : Vector,
...
}
type alias World = List Entity
type alias ComponentCreator = Entity -> Entity
@TheSeamau5
TheSeamau5 / NewRecordEntityComponentSystem.elm
Last active June 12, 2017 03:23
Example under the New Record Entity Component System
import List (map, (::))
import Color (Color, rgb)
import Keyboard (arrows)
import Signal (Signal, (<~), foldp)
import Graphics.Collage (square, circle, move, filled, collage, Form)
import Graphics.Element (Element)
type alias Vector = {
x : Float,
y : Float
@TheSeamau5
TheSeamau5 / UpdatePosition.coffee
Created December 30, 2014 04:30
Example of using updateComponent
Vector = (x,y) ->
x : x || 0,
y : y || 0
vAdd = (v,w) -> Vector (v.x + w.x), (v.y + w.y)
clone = (obj) ->
if not obj? or typeof obj isnt 'object'
return obj
@TheSeamau5
TheSeamau5 / UpdateComponent.coffee
Created December 30, 2014 04:28
General Update Component in Coffeescript
updateComponent = (updater, componentName, entity, requirementList) ->
requirementList = requirementList || [];
if (requirementList[requirement] for requirement in requirementList)?
output = clone entity
output[componentName] = updater output[componentName], output
return output
@TheSeamau5
TheSeamau5 / EntityComponentSystemExploration.md
Created December 29, 2014 23:16
An exploration of the Entity Component System in Elm

#Exploring Entity Component Systems in Elm

Entity-Component-System (or ECS) is a pattern for designing programs that is prevalent in the games industry. This pattern consists of three simple parts:

  • Entity : A uniquely identifiable object that may contain any number of components
  • Component : A property usually representing the raw data of one aspect of the object. (Position is a component, Velocity is a component, Strength is a component, etc...)
  • System : A continuous process performing actions on every entity that possesses a component of the same aspect as that system

To understand this, let us try to make a simple example: Boxes that move in space: