Skip to content

Instantly share code, notes, and snippets.

View ninjarobot's full-sized avatar

Dave Curylo ninjarobot

  • Microsoft @Azure
  • Atlanta, GA
View GitHub Profile
@ninjarobot
ninjarobot / TextToImage.fs
Created January 27, 2016 19:01
Image from text in fsharp
open System.Drawing
[<EntryPoint>]
let main argv =
let text = argv.[0]
use font = new Font("Courier New", 72.0f * 5.0f, FontStyle.Bold)
let getTextSize str =
use b = new Bitmap (1, 1)
use g = b |> Graphics.FromImage
g.MeasureString (str, font)
@ninjarobot
ninjarobot / git-hash-object.fs
Last active May 16, 2016 20:55
Git object hash in FSharp
let calcObjHash (objData:byte array) =
use sha1 = new System.Security.Cryptography.SHA1Managed ()
let pre =
sprintf "blob %d\000" objData.Length
|> System.Text.Encoding.UTF8.GetBytes
use ms = new System.IO.MemoryStream ()
ms.Write (pre, 0, pre.Length)
ms.Write (objData, 0, objData.Length)
ms.Position <- 0L
sha1.ComputeHash (ms)
@ninjarobot
ninjarobot / Program.fs
Last active May 23, 2016 17:46
Simple kestrel-based ASP.NET server in FSharp on dotnet core RC2
(*
* To get started, `dotnet new --lang f#`
* Add these to the 'dependencies' section in project.json:
"System.Threading.Tasks": "4.0.11-rc2-*",
"Microsoft.AspNetCore.Hosting":"1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Console":"1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.Kestrel":"1.0.0-rc2-final"
* And then edit Program.fs with reqHandler for the RequestDelegate,
* wrapping an async workflow.
*)
UnixSignal.WaitAny ([|UnixSignal (Signum.SIGINT); UnixSignal (Signum.SIGTERM); UnixSignal (Signum.SIGQUIT)|], -1)
# From debian, e.g. `docker run -it debian bash`
apt-get update
apt-get install -y ocaml-mingw-w64 gcc-mingw-w64
echo 'print_string "Hello world!\n";;' >> hello.ml
/usr/bin/i686-w64-mingw32-ocamlopt -o hello.exe hello.ml
@ninjarobot
ninjarobot / githubapi.pl
Created February 3, 2017 03:43
Retrieves some public repository information via SWI-Prolog.
:- module(githubapi, [list_repos/3]).
:- use_module(library(http/http_client)).
:- use_module(library(http/http_json)).
process_repo(J) :-
J = json(D),
format(
'~w \n\t~w \n\tlanguage: ~w \n\tclone url: ~w \n',
[D.name, D.description, D.language, D.clone_url]
@ninjarobot
ninjarobot / dotnetcore2.Dockerfile
Created May 10, 2017 12:58
Dockerfile for latest dotnetcore 2.0 runtime - based on Alpine
FROM frolvlad/alpine-glibc
RUN apk add --no-cache curl libstdc++ --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ \
&& curl -O https://dotnetcli.blob.core.windows.net/dotnet/master/Binaries/Latest/dotnet-linux-x64.latest.tar.gz \
&& mkdir -p /usr/share/dotnet \
&& tar -xzvf dotnet-linux-x64.latest.tar.gz -C /usr/share/dotnet \
&& rm dotnet-linux-x64.latest.tar.gz \
&& ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet
@ninjarobot
ninjarobot / mkbundle-alpine-example.Dockerfile
Created June 19, 2017 17:41
A minimal Alpine image using a self-contained .NET executable built with mkbundle.
FROM alpine
RUN echo 'using System; \
namespace Program { \
public class MyClass { \
public static void Main(string[] args) { \
Console.WriteLine ("Hello from C#"); \
} \
} \
}' >> Program.cs
RUN apk add --virtual build-dependencies --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing --no-cache mono mono-dev musl-dev binutils gcc && \
  • Unrecognized configuration section 'runtime' - this happens when machine.config isn't included. Use --machine-config /etc/mono/4.5/machine.config in addition to --config /etc/mono/config
  • Some libraries are under /usr/lib/mono/4.5/Facade - things like System.Threading.Task.Extensions.dll. If these are used, then be sure to include them.
@ninjarobot
ninjarobot / currying.fs
Created August 11, 2017 21:19
F# currying example
let add2 num1 num2 =
num1 + num2
// The compiler will do this:
let addTwo num1 =
fun num2 ->
num1 + num2
// Both functions work the same, but the curried form accepts a single parameter and returns a function that accepts another parameter.