Skip to content

Instantly share code, notes, and snippets.

View chribben's full-sized avatar

Christian Jacobsen chribben

View GitHub Profile
@chribben
chribben / BouncingRandomTetrisBrick.elm
Created August 14, 2013 20:45
BouncingRandomTetrisBrick
import Random
data Brick = Line | LeftSquiggle | RightSquiggle | LeftStick | RightStick | Box
rand = Random.range 0 6 (constant 1)
brick n = if | n == 0 -> Line
| n == 2 -> LeftSquiggle
| n == 3 -> RightSquiggle
| n == 4 -> RightStick
| n == 5 -> LeftStick
| otherwise -> Box
brickText = asText <~ (brick <~ rand)
@chribben
chribben / TetrisBrickGenerator.elm
Created August 13, 2013 21:18
Tetris Brick Generator
import Random
data Brick = Line | LeftSquiggle | RightSquiggle | LeftStick | RightStick | Square
rand a = Random.range 0 6 (constant a)
brick n = if | n == 0 -> Line
| n == 2 -> LeftSquiggle
| n == 3 -> RightSquiggle
| n == 4 -> RightStick
| n == 5 -> LeftStick
| otherwise -> Square
main = asText <~ (brick <~ (rand 1))
time = lift (inSeconds . fst) (timestamp (fps 40))
a dy = let c = filled blue (circle 10)
in collage 300 300
[c |> move (0, 100 * cos dy)]
b = lift a time
main = b
@chribben
chribben / ReactiveCircle.elm
Created August 11, 2013 21:16
Reactive circle
import Signal
import Mouse
cl = (clamp 100 200) <~ Mouse.x
reactiveCircle = (filled blue) <~ (circle <~ (toFloat <~ cl))
main = lift (collage 300 300) (Signal.combine [reactiveCircle])
@chribben
chribben / FuncPuzzle4.fsx
Last active December 20, 2015 03:59
1. Write a function that, given a sequence of either positive or negative integers, returns true if any subset of the list sums up to zero. Eg: find-solutions ([-7 4 3 8 -5]) => true find-solutions ([-6 2 3]) => false 2. Make it work with any number, not just zero. Eg: find-solutions (0, [-7 4 3 8 -5]) => true find-solutions (13, [-7 4 3 8 -5]) …
let initMask length = List.init length (fun i -> if i = length - 1 then 1 else 0)
let addOne mask = List.foldBack (fun t (resList, c) -> (if (t = 1 && c = 1) then (0::resList, 1) elif (t = 0 && c= 0) then (0::resList, 0) else (1::resList, 0))) mask ([], 1) |> fst
let subsetsWhosSumsSatisfiesPredicate lst pred =
let rec work mask = seq{
let sum, subSet = List.fold2 (fun (accSum, subSet) m l -> if m = 0 then (accSum, subSet) else (accSum + l, l::subSet)) (0, []) mask lst
if pred sum then
yield (List.rev subSet)
if List.exists (fun t -> t = 0) mask then
yield! work (addOne mask)
}
@chribben
chribben / MessagePassingFSharp.fsx
Created April 9, 2013 20:32
Tcp message passing in F#
open System.Net.Sockets
open System.IO
open System
let port = 1234
let clientProc i =
printfn "Connecting"
let client = new System.Net.Sockets.TcpClient("192.168.48.43", port)
let inew = i+uint64(1)
let sw = new StreamWriter(client.GetStream())
[STAThread]
static void Main()
{
_nui = new Runtime();
var app = new Application();
var window = new Window();
InitializeNui(); //Initializing the Runtime object and opening video streams
CreateGUI(window); //Setting up a canvas to hold the RGB video and the image attached to the hand of captured person
var skeletonFrameReadyObservable = Observable.FromEventPattern(_nui, "SkeletonFrameReady");
var trackedSkeletons = from ev in skeletonFrameReadyObservable
class mosquitto {
exec { 'apt-get update':
command => '/usr/bin/apt-get update'
}
package { 'python-software-properties':
ensure => installed,
require => Exec['apt-get update'],
}
exec { 'sudo add-apt-repository ppa:mosquitto-dev/mosquitto-ppa':
command => 'add-apt-repository ppa:mosquitto-dev/mosquitto-ppa',
public interface IRepository<TEntity> where TEntity : class
{
IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "");
TEntity GetById(object id);
void AddOrUpdate(TEntity entity);
void Delete(object id);
@chribben
chribben / ImageCaptureHandler.cs
Created October 7, 2012 10:09
Async gotcha in C# III
async void ImageCaptured(Dto dto)
{
dto.Id = Guid.NewGuid().ToString();
var file = await _fileHndlr.CreateFileAsync(dto.Id);
dto.ImageFilePath = file.Path;
_fileOperator.StoreStream(dto.ImageStream, file);
SaveNewDataItem(dto);
var dataItem = dataSource.GetItem(dto.Id);
StoreData(dataItem);
}