Skip to content

Instantly share code, notes, and snippets.

@rasmusvhansen
rasmusvhansen / typescript.ts
Created November 18, 2019 20:59
Including exhaustiveCheck
interface BankAccount {
readonly id: string;
readonly name: string;
readonly number: string;
readonly balance: number;
readonly secret: boolean;
}
interface UpdateAccount {
readonly kind: 'update';
@rasmusvhansen
rasmusvhansen / typescript.ts
Created November 18, 2019 20:20
Typescript demo
interface BankAccount {
readonly id: string;
readonly name: string;
readonly number: string;
readonly balance: number;
readonly secret: boolean;
}
interface UpdateAccount {
readonly kind: 'update';
const source = Array(1000000).fill(1).map((_, i) => Math.random());
console.time('array chaining');
const arrayResult = source
.map(n => n * 2)
.filter(n => n > 0.5)
.reduce((m, c) => Math.max(m, c));
console.timeEnd('array chaining'); // 310.901ms
console.time('rxjs transducer');
@rasmusvhansen
rasmusvhansen / infinite.ts
Created January 26, 2019 22:06
Infinite RxJS transducer
function* integers() {
let i = 0;
while (true) {
yield i++;
}
}
const result = transducer(integers())(
skip(10),
filter(n => n % 3 === 0),
@rasmusvhansen
rasmusvhansen / rxjs-transducer.ts
Created January 26, 2019 22:01
RxJS Transducer Usage
import { transducer } from 'rxjs-transducer';
const result = transducer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])(
map(n => n * 2),
filter(n => n > 4),
map(n => ' Num: ' + n),
reduce((acc, str) => acc + str, 'Start:')
);
console.log(result);
@rasmusvhansen
rasmusvhansen / rxjs-array-xform.ts
Created January 26, 2019 21:55
RxJS Array Transformation
let result;
from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).pipe(
map(n => n * 2),
filter(n => n > 4),
map(n => ' Num: ' + n),
reduce((acc, str) => acc + str, 'Start:')
).subscribe(r => result = r);
console.log(result); // Start: Num: 6 Num: 8 Num: 10 Num: 12 Num: 14 Num: 16 Num: 18 Num: 20
@rasmusvhansen
rasmusvhansen / arrayMethods.ts
Last active January 26, 2019 21:54
Array methods
const result = [1,2,3,4,5,6,7,8,9,10]
.map(n => n * 2)
.filter(n => n > 4)
.map(n => ' Num: ' + n)
.reduce((acc, str) => acc + str, 'Start:');
console.log(result); // Start: Num: 6 Num: 8 Num: 10 Num: 12 Num: 14 Num: 16 Num: 18 Num: 20
@rasmusvhansen
rasmusvhansen / Clock.elm
Last active September 19, 2016 19:47
ELM Clock with minute and hour hand
-- Paste into http://elm-lang.org/try and press compile to try it.
import Html exposing (Html, div)
import Html.App as App
import Svg exposing (..)
import Svg.Attributes exposing (..)
import Time exposing (Time, second)
main =
function AttachmentCtrl($scope, $location, $timeout, Docs) {
$(function() {
$('#detail-form-doc').fileupload({
dataType: 'json',
url: '/angular-ib/app/fileupload?id=' + $location.search().id,
add: function(e, data) {
$scope.$apply(function(scope) {
// Turn the FileList object into an Array
for (var i = 0; i < data.files.length; i++) {
$scope.project.files.push(data.files[i]);