Skip to content

Instantly share code, notes, and snippets.

public static Option<TException> WithoutValue<TValue, TException>(this Option<TValue, TException> option)
{
return option.Match(_ => Option.None<TException>(), exception => exception.Some());
}
public static IEnumerable<TValue> Values<TValue, TException>(this IEnumerable<Option<TValue, TException>> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return source.SelectMany(option => option.ToEnumerable());
}
@nojaf
nojaf / Program.fs
Created August 9, 2017 13:57
Computation Expressions question
// Learn more about F# at http://fsharp.org
// See the 'F# Tutorial' project for more help.
open System
let toInt a =
match (Int32.TryParse(a)) with
| (true, x) -> Some x
| (false, _) -> None
type MaybeBuilder() =
@nojaf
nojaf / maybe.fs
Created August 9, 2017 06:51
F# Computation Expressions
open System
let divideBy bottom top =
if bottom = 0
then None
else Some(top/bottom)
type MaybeBuilder() =
member this.Bind(x, f) =
@nojaf
nojaf / Result.fs
Created August 8, 2017 06:54
ResultBuilder
module Result =
type Result<'TResult, 'TError> =
| Ok of 'TResult
| Error of 'TError
let ifNone (error : 'TError) = function
| Some (o) -> Ok o
| None -> Error error
let map f = function
@nojaf
nojaf / MaybeBuilder.fs
Created August 8, 2017 06:45
Maybe Builder
type MayBeBuilder() =
member x.Bind(v,f) = Option.bind f v
member x.Return v = Some v
member x.ReturnFrom o = o
let maybe = OptionBuilder()
@nojaf
nojaf / array.ts
Created July 12, 2017 12:55
Chunk & flatten
export function flatten<T>(array: T[][]): T[] {
return [].concat.apply([], array);
}
export function chunk<T>(
array: T[],
chunkSize: number,
acc: T[][] = []
): T[][] {
return array.length > chunkSize
@nojaf
nojaf / index.d.ts
Created July 12, 2017 12:54
react-throttle.d.ts
declare module "react-throttle" {
export interface DebounceProps extends React.Props<Debounce> {
time: number;
handler: string;
}
export class Debounce extends React.Component<DebounceProps> {}
}
@nojaf
nojaf / Query.fs
Created May 5, 2017 14:01
Multiple joins with F# SqlProvider
let getXsd id =
query {
for messageItem in ctx.ProtoType.MessageType do
for originalMessage in messageItem.``ProtoType.OriginalMessage by Id`` do
for messageType in originalMessage.``ProtoType.MessageType by Id`` do
where (messageItem.Id = id)
select (messageType.Xsd)
} |> Seq.head
@nojaf
nojaf / Program.fs
Last active May 10, 2017 06:34
Giraffe file upload
open Giraffe.HttpHandlers
open Microsoft.AspNetCore.WebUtilities
open System.IO
open System
let isMultipartContentType (contentType:string) =
not(String.IsNullOrEmpty(contentType)) &&
contentType.IndexOf("multipart/", StringComparison.OrdinalIgnoreCase) >= 0
let getBoundary (contentType : string) =
@nojaf
nojaf / dom-selector.js
Created April 14, 2017 07:48
minimal DOM selector function
const $ = (selector) => {
return Array.prototype.slice.call(document.querySelectorAll(selector));
}