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 / SimpleRotatingCube.elm
Created December 5, 2014 06:03
Simple Rotating Cube in Elm
import Graphics.WebGL (..)
import Math.Vector3 (..)
import Math.Matrix4 (..)
type Attribute = {position : Vec3}
type Uniform = { rotationMatrix : Mat4}
type Varying = {}
mapMesh : (a -> b) -> [Triangle a] -> [Triangle b]
mapMesh = map << mapTriangle
@TheSeamau5
TheSeamau5 / Quickcheck.elm
Last active August 29, 2015 14:11
Quickcheck in Elm
quickCheck : Generator a -> Int -> (a -> Bool) -> String
quickCheck randomGenerator numberOfCases testingCondition =
let listGenerator = list numberOfCases <| randomGenerator
testInputs = fst <| generate listGenerator (initialSeed 1)
getOutput input = (input, testingCondition input)
testOutputs = map getOutput testInputs
failingOutputs = filter (\x -> (snd x) == False) testOutputs
successString = "Ok, passed " ++ (toString numberOfCases) ++ " tests."
failingString fail = "The following input has failed the test: " ++ (toString fail)
in
@TheSeamau5
TheSeamau5 / Interleave.elm
Last active August 29, 2015 14:11
Interleave two lists in Elm
interleave : List a -> List a -> List a
interleave list1 list2 =
case list1 of
[] -> list2
x :: xs ->
case list2 of
[] -> list1
y :: ys -> y :: x :: interleave xs ys
@TheSeamau5
TheSeamau5 / CatmullClarkSubdivision2D.elm
Last active August 29, 2015 14:11
2D Catmull Clark Subdivision in Elm
import Graphics.Collage (move, filled, circle, Form, collage)
import Graphics.Element (Element)
import List (map, map2, (::), head, tail)
import Color (rgb)
--- GLOBALS YOU CAN MESS WITH
pointSize = 3 -- THE SIZE OF THE POINTS
pointColor = rgb 255 100 0 -- THE COLOR OF THE POINTS
iterations = 5 -- NUMBER OF ITERATIONS OF CATMULL-CLARK SUBDIVISION
@TheSeamau5
TheSeamau5 / MapAll.elm
Created December 17, 2014 23:12
MapAll Function in Elm. Applies a 2-ary function to each element in a list to each element in a list
mapAll : (a -> a -> a) -> List a -> List (List a)
mapAll f list =
let innerMapAll f tempList =
case tempList of
[] -> []
x :: xs -> map (f x) list :: innerMapAll f xs
in innerMapAll f list
@TheSeamau5
TheSeamau5 / PrettyBug.elm
Created December 18, 2014 02:29
Tried to make a sphere and all I got was this bug
sphereMesh : Int -> Vec3 -> Float -> Mesh
sphereMesh tesselation center radius =
let normalize = (flip (/)) (toFloat tesselation)
shift = (+) -0.5
scale = (*) (2 * radius)
transform = normalize >> shift >> scale
latitudes = map transform (map toFloat [0..tesselation])
longitudes = latitudes
plotPoint longitude latitude =
let cosLatitude = cos (pi * latitude / (toFloat tesselation))
@TheSeamau5
TheSeamau5 / Tuple.cpp
Created December 25, 2014 02:58
Tuples in C++
#include <string>
using namespace std;
template <typename First, typename Second>
struct Tuple{
First first;
Second second;
};
@TheSeamau5
TheSeamau5 / Compute.elm
Created December 29, 2014 15:29
Function to perform a given operation a given number of times
compute : Int -> (a -> a) -> a -> a
compute iterations function argument =
if (iterations <= 0) then argument
else compute (iterations - 1) function (function argument)
@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 / 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