Skip to content

Instantly share code, notes, and snippets.

View sblom's full-sized avatar

Scott Blomquist sblom

View GitHub Profile
@sblom
sblom / life.ijs
Created March 17, 2011 18:15
A straight J port of Dyalog's APL implementation of Conways's game of life (http://www.youtube.com/watch?v=a9xAKttWgP4)
life =: 3 : '+./ (1 ,: y) *. 3 4="0 _ +/ +/ 1 0 _1 |."1/"(0 _) 1 0 _1 |./"0 _ y'
@sblom
sblom / VirtualAccessors.cs
Created November 30, 2011 01:26
IronPython virtual getter/setter bug
public class Base {
public virtual int Accessor {
get; set;
}
}
public class GetOverride: Base {
public override int Accessor {
get {
return base.Accessor - 1;
@sblom
sblom / Problem1.ma
Created February 11, 2012 06:47
Embedly challenge (in Mathematica)
n = 1; While[Total[IntegerDigits[n!]] != 8001, n = n + 1]; n
@sblom
sblom / Example1.m
Created April 1, 2012 01:09
Lazy list implementation for Mathematica
(* Project Euler Problem 1 *)
(* Find the sum of all the multiples of 3 or 5 below 1000. *)
(* StreamSource[#&] is a simple idiom for "a stream of all the natural numbers". *)
ints = TakeWhile[StreamSource[# &], # < 1000 &];
(* Choose only the numbers that are multiples of 3 or 5. *)
filtered = Select[ints, Mod[#, 3] == 0 || Mod[#, 5] == 0];
(* Sum them. Will probably add Stream/:Total[] since it comes up a lot. *)
@sblom
sblom / fracgen.m
Created April 12, 2012 17:47
Stern–Brocot generator: generate all positive rational numbers
frac[1] = {{0, 1}, {1, 1}, {1,0}}
frac[n_] := frac[n] = frac[n - 1] ~Riffle~ newFracs[n - 1]
newFracs[n_] := Apply[Plus, #] & /@ Partition[frac[n], 2, 1]
(* Example usage: *)
Map[Apply[Divide, #] &, frac[14]]
(*
A few very surprising things happen here:
@sblom
sblom / clojure-reducer.linq
Last active January 25, 2021 19:31
Toy implementation of Clojure 1.5's reducers library in C# (using LINQPad). https://www.clojure.org/news/2012/05/15/anatomy-of-reducer
List<int> ns = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
Func<Func<int,int>,Func<Func<int,int,int>,Func<int,int,int>>> mapping = delegate(Func<int,int> f) {
return (Func<int,int,int> f1) => ((int r, int i) => f1(r,f(i)));
};
Func<Func<int,bool>,Func<Func<int,int,int>,Func<int,int,int>>> filtering = delegate(Func<int,bool> pred) {
return (Func<int,int,int> f1) => ((int r, int i) => {
if (pred(i))
return f1(r,i);
@sblom
sblom / GitHubForWindowsPath.cmd
Last active July 26, 2022 19:54
Crazy hack to extract the GitHub for Windows %PATH% modifications out of shell.ps1 for use in cmd.exe.
for /f "usebackq delims=*" %%p in (`powershell -ExecutionPolicy RemoteSigned -Command "& { . $env:LOCALAPPDATA\GitHub\shell.ps1 ; echo $env:path }"`) do set path=%%p
@sblom
sblom / TotalHours.excel
Last active October 30, 2023 22:17
Native Excel array function to take a horizontal array of clock-in/clock-out times and return total hours
=LET(MakeAcc,LAMBDA(item1,item2,MAKEARRAY(2,1,LAMBDA(row,col,IF(row=1,item1,item2)))),
LAMBDA(arr,INDEX(REDUCE({0;-1},FILTER(arr,MAP(arr,LAMBDA(x,ISNUMBER(x))),{0}),LAMBDA(accum,time,
LET(total,INDEX(accum,1),start,INDEX(accum,2),
IF(start<>-1,
MakeAcc(total+(time-start)*24,-1),
MakeAcc(total,time)
)
))),1)))