1 people like it.

Placeholder syntax for binary operators

Placeholder syntax like scala or clojure. It can be used when the situation point-free style can't be applied. It only works for binary operators. But it's quite simple and can be used somewhat.

Implementation

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
open System
type Placeholder () =
  static member inline (*) (_:Placeholder, rhs) = fun lhs -> lhs * rhs
  static member inline (/) (_:Placeholder, rhs) = fun lhs -> lhs / rhs
  static member inline (+) (_:Placeholder, rhs) = fun lhs -> lhs + rhs
  static member inline (-) (_:Placeholder, rhs) = fun lhs -> lhs - rhs
  static member inline (%) (_:Placeholder, rhs) = fun lhs -> lhs % rhs

let __1 = Placeholder ()
/// it's same as swap though..
let _1 binOp rhs lhs = binOp lhs rhs

let printHex (n:int) = (...)

Usage

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
0xff             |> printHex
|> __1 % 0x10    |> printHex
|> __1 * 0x10    |> printHex
|> __1 / 0x04    |> printHex
|> __1 - 0x30    |> printHex
|> __1 + 0xc0    |> printHex
|> _1 (|||) 0x33 |> printHex
|> _1 (&&&) 0x55 |> printHex
|> _1 (<<<) 1    |> printHex
|> _1 (>>>) 1    |> printHex
|> ignore

Result

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
//0b11111111
//0b00001111
//0b11110000
//0b00111100
//0b00001100
//0b11001100
//0b11111111
//0b01010101
//0b10101010
//0b01010101
namespace System
Multiple items
type Placeholder =
  new : unit -> Placeholder
  static member ( + ) : Placeholder * rhs:'g -> ('h -> 'i) (requires member ( + ))
  static member ( / ) : Placeholder * rhs:'j -> ('k -> 'l) (requires member ( / ))
  static member ( % ) : Placeholder * rhs:'a -> ('b -> 'c) (requires member ( % ))
  static member ( * ) : Placeholder * rhs:'m -> ('n -> 'o) (requires member ( * ))
  static member ( - ) : Placeholder * rhs:'d -> ('e -> 'f) (requires member ( - ))

Full name: Script.Placeholder

--------------------
new : unit -> Placeholder
val rhs : 'm (requires member ( * ))
val lhs : 'n (requires member ( * ))
val rhs : 'j (requires member ( / ))
val lhs : 'k (requires member ( / ))
val rhs : 'g (requires member ( + ))
val lhs : 'h (requires member ( + ))
val rhs : 'd (requires member ( - ))
val lhs : 'e (requires member ( - ))
val rhs : 'a (requires member ( % ))
val lhs : 'b (requires member ( % ))
val binOp : ('a -> 'b -> 'c)
val rhs : 'b
val lhs : 'a
val printHex : n:int -> int

Full name: Script.printHex
val n : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
"0b" + Convert.ToString(n,2).PadLeft(8,'0')
  |> printfn "%s"
  n
val __1 : Placeholder

Full name: Script.__1
val _1 : binOp:('a -> 'b -> 'c) -> rhs:'b -> lhs:'a -> 'c

Full name: Script._1


 it's same as swap though..
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore

More information

Link:http://fssnip.net/j4
Posted:4 years ago
Author:nagat01
Tags: scala , clojure , lambda