Skip to content

Instantly share code, notes, and snippets.

@dezfowler
dezfowler / Program.cs
Created Jul 8, 2020
Serialize to XDocument benchmark
View Program.cs
using System.IO;
using System.Text;
using System.Xml.Linq;
using System.Xml.Serialization;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace MyBenchmarks
{
[MemoryDiagnoser]
@dezfowler
dezfowler / safe-switch.ts
Last active Aug 6, 2019
Typed visitor discriminated union
View safe-switch.ts
function calculateArea(shape: Shape): number {
switch(shape.__typename) {
case 'Circle':
return Math.PI * (shape.radius ^ 2);
case 'Rectangle':
return shape.width * shape.height;
case 'Square':
return shape.side ^ 2;
case 'Triangle':
return (shape.base / 2) * shape.height;
@dezfowler
dezfowler / array-assert-1.ts
Last active Jul 24, 2019
TypeScript type checking fails
View array-assert-1.ts
// No error with incorrect 'botton' item
var x1 = <Alignment[]>['top', 'top', 'botton'];
// No error with different underlying value number
var x2 = <Alignment[]>['top', 'top', 2];
// Correctly errors with
// Type '"botton"' is not assignable to type 'Alignment'.
var x3: Alignment[] = ['top', 'top', 'botton'];
View rxify.js
import { Subject } from 'rxjs';
import { tap } from 'rxjs/operators';
export const rxify = renderStreamFactory =>
class Rxified extends React.Component {
constructor(props) {
super(props);
this.state = null;
}
@dezfowler
dezfowler / redux-obs-example.js
Created Apr 4, 2019
redux-observable simple example
View redux-obs-example.js
import { ofType } from 'redux-observable';
const pingEpic = action$ => action$.pipe(
ofType('PING'),
delay(1000), // Asynchronously wait 1000ms then continue
mapTo({ type: 'PONG' })
);
@dezfowler
dezfowler / Clock.jsx
Last active Mar 29, 2019
Simple rxified Clock
View Clock.jsx
const { interval, empty } = require('rxjs');
const { switchMap, startWith, map } = require('rxjs/operators');
const Clock = rxify((initialProps, prop$) => {
return prop$
.pipe(
switchMap(props => props.running ? interval(0, 1000) : empty()),
startWith(1),
map(_ => (<span className="time">{new Date().toLocaleTimeString()}</span>))
)
@dezfowler
dezfowler / Clock.jsx
Created Dec 14, 2018
Observable Clock.jsx
View Clock.jsx
class Clock extends React.Component {
constructor(props) {
super(props);
console.log('new');
this.state = { time: new Date() };
}
componentDidMount() {
this.propsChangeSubject = new Subject();
this.subscription = this.propsChangeSubject
@dezfowler
dezfowler / Clock.jsx
Created Dec 14, 2018
Async Clock.jsx
View Clock.jsx
class Clock extends React.Component {
constructor(props) {
super(props);
this.intervalId = undefined;
this.state = { time: new Date() };
}
componentWillUnmount() {
this.stopTick();
}
@dezfowler
dezfowler / Clock.jsx
Last active Dec 13, 2018
rxified Clock
View Clock.jsx
const Clock = rxify((initialProps, prop$) => {
return prop$
.pipe(
filter(pd => pd.differences.includes('running')),
map(pd => pd.next.running),
switchMap(running => running ? interval(0, 1000) : empty()),
startWith(1),
map(_ => (<span className="time">{new Date().toLocaleTimeString()}</span>))
)
});
@dezfowler
dezfowler / BlockingCollectionExample.cs
Created Jan 9, 2018
Throttling with BlockingCollection example
View BlockingCollectionExample.cs
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace myApp
{
class Program