Skip to content

Instantly share code, notes, and snippets.

View vivainio's full-sized avatar

Ville Vainio vivainio

View GitHub Profile
open System
module Ast =
type var = string
type Expr =
| Number of float
| BinOp of (float -> float -> float) * Expr * Expr
| FunApply of var * Expr list
with
static member Sum (e1, e2) = BinOp (( + ), e1, e2)
open FSharp.Data
open System.IO
type provider = XmlProvider<"./sample.xml">
[<EntryPoint>]
let main argv =
let path = @"C:\blah.csproj"
let f = File.ReadAllText path
let data = provider.Parse f
public class Collector<TKey>
{
private HashSet<TKey> seen = new HashSet<TKey>();
public IEnumerable<TData> CollectBy<TData>(IEnumerable<TData> input, Func<TData, TKey> extractKey)
{
foreach (var inp in input)
{
var key = extractKey(inp);
if (seen.Contains(key))
open System.IO
open OfficeOpenXml
open System.Data
[<EntryPoint>]
let main argv =
let f = FileInfo("test.xlsx")
use p = new ExcelPackage(f)
let ws = p.Workbook.Worksheets.Add("testi")
use dt = new DataTable("mundata")
// deprecate a set of properties. Handy e.g. to see bad access in angular directives where TypeScript doesn't help you
class FooClass { ... }
function deprecate(klass: any, props: string[]) {
props.forEach(prop => {
Object.defineProperty(klass.prototype, prop, {
get: () => { debugger; console.log('BAD READ', prop) },
set: () => console.log('BAD WRITE', prop)
})
})
[
{ "key": "ctrl+left", "command": "cursorHome" },
{ "key": "ctrl+right", "command": "cursorEnd" }
]
@vivainio
vivainio / checkedtranslate.ts
Last active June 16, 2016 15:14
Typescript replacement for $translate.instant()
class CheckedTranslate {
static $inject = ['$rootScope', '$translate']
constructor(private $rootScope: ng.IRootScopeService, private $translate: ng.translate.ITranslateService) {}
translate(localizations: any) {
// obviously this mutates passed localizations object behind your back
this.$rootScope.$on('$translateChangeSuccess', () => {
for (var key in localizations) {
localizations[key] = this.$translate.instant(localizations[key]);
}
})
emphasis { font-style: italic; }
strong { font-weight: bold; }
header { color: rgba(0, 0, 128, 1); }
comment { color: rgba(96, 139, 78, 1); }
constant.language { color: rgba(86, 156, 214, 1); }
constant.numeric { color: rgba(181, 206, 168, 1); }
constant.regexp { color: rgba(100, 102, 149, 1); }
constant.rgb-value { color: rgba(212, 212, 212, 1); }
entity.name.tag { color: rgba(86, 156, 214, 1); }
entity.name.function { color: rgba(212, 212, 212, 1); }

FSharp intro

Why

Less code, little to no C# like source bloat because of:

  • Type inference
  • Automatic generics
  • Functions instead of methods
  • seqs and tuples + unpacking (a'la python)
  • pattern matching ("switch case" that matches types & unpacks)
(f => {
try {
document.write(f("Vampire"));
}
catch (e) {};
})(function SaveWorld(startup) {
return world.savedBy(startup);
});