7 people like it.

APL mode for F#

Inspired by @theburningmonk, this snippet defines a couple of extensions and operators that can be used to make F# as short and as obscure as APL. Complete with a 19 character solution of the Euler problem #1!

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
module AplMode =
  let (%) l n = List.map (fun v -> v % n) l
  let (=) l b = List.map (fun v -> v = b) l
  let (||) l1 l2 = List.zip l1 l2 |> List.map (fun (a, b) -> a || b)
  let (~+) l = List.reduce (+) l

  type List<'T> with 
    member x.Item with get(a) = List.zip a x |> List.filter fst |> List.map snd

open AplMode
let w = [1..999]

// See: http://theburningmonk.com/2015/07/apl-solving-euler-problem-1/
+(w.[w%3=0||w%5=0])
val l : int list
val n : int
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val v : int
val l : 'a list (requires equality)
val b : 'a (requires equality)
val v : 'a (requires equality)
val l1 : bool list
val l2 : bool list
val zip : list1:'T1 list -> list2:'T2 list -> ('T1 * 'T2) list

Full name: Microsoft.FSharp.Collections.List.zip
val a : bool
val b : bool
val reduce : reduction:('T -> 'T -> 'T) -> list:'T list -> 'T

Full name: Microsoft.FSharp.Collections.List.reduce
val x : List<'T>
member List.Item : a:bool list -> 'T list with get

Full name: Script.AplMode.Item
val a : bool list
val filter : predicate:('T -> bool) -> list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.filter
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
module AplMode

from Script
val w : int list

Full name: Script.w

More information

Link:http://fssnip.net/rU
Posted:9 years ago
Author:Tomas Petricek
Tags: apl , operators