Skip to content

Instantly share code, notes, and snippets.

@wraikny
Last active April 20, 2023 23:16
Show Gist options
  • Save wraikny/fe063563d52c684422a7dd14a0cf2145 to your computer and use it in GitHub Desktop.
Save wraikny/fe063563d52c684422a7dd14a0cf2145 to your computer and use it in GitHub Desktop.
F# Apply pattern
(*
MIT License
Copyright (c) 2023 wraikny
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*)
open System.ComponentModel
[<EditorBrowsable(EditorBrowsableState.Never)>]
module Internal =
(* 任意長のタプルをサポートする SRTP 制約の書き方 by cannorin - Qiita https://qiita.com/cannorin/items/aaf6abb2a7c1bc7793d4 *)
let inline whenNestedTuple (t: 't) =
(^t: (member Item1: 't1) t), (^t: (member Item2: 't2) t), (^t: (member Item3: 't3) t),
(^t: (member Item4: 't4) t), (^t: (member Item5: 't5) t), (^t: (member Item6: 't6) t),
(^t: (member Item7: 't7) t), (^t: (member Rest: 'tr) t)
type ComposeFunctions =
static member inline Invoke fs : ('i -> 'o) =
let inline call_2 (a: ^a, b: ^b) = ((^a or ^b) : (static member ComposeFunctions: _*_ -> _) b, a)
let inline call (a: 'a, b: 'b) = call_2 (a, b)
call (Unchecked.defaultof<ComposeFunctions>, fs)
static member inline ComposeFunctions (t: 't, ct: ^ComposeFunctions) =
let f1,f2,f3,f4,f5,f6,f7,tr : _*_*_*_*_*_*_* ^TR = whenNestedTuple t
f1 >> f2 >> f3 >> f4 >> f5 >> f6 >> f7 >> ((^TR or ^ComposeFunctions): (static member ComposeFunctions: _*_->_) tr,ct)
static member inline ComposeFunctions (t: System.Tuple<'a -> 'b>, _: ComposeFunctions) = t.Item1
static member inline ComposeFunctions ((f1 : 'a -> 'b), _: ComposeFunctions) = f1
static member inline ComposeFunctions ((f1, f2), _: ComposeFunctions) = f1 >> f2
static member inline ComposeFunctions ((f1, f2, f3), _: ComposeFunctions) = f1 >> f2 >> f3
static member inline ComposeFunctions ((f1, f2, f3, f4), _: ComposeFunctions) = f1 >> f2 >> f3 >> f4
static member inline ComposeFunctions ((f1, f2, f3, f4, f5), _: ComposeFunctions) = f1 >> f2 >> f3 >> f4 >> f5
static member inline ComposeFunctions ((f1, f2, f3, f4, f5, f6), _: ComposeFunctions) = f1 >> f2 >> f3 >> f4 >> f5 >> f6
static member inline ComposeFunctions ((f1, f2, f3, f4, f5, f6, f7), _: ComposeFunctions) = f1 >> f2 >> f3 >> f4 >> f5 >> f6 >> f7
let inline (|Apply|) (f: 'f) x = Internal.ComposeFunctions.Invoke f x
(* Example:
let f : Choice<int[], int list> -> int = function
| Choice1Of2 (Apply (Array.map ((+) 1), Array.toList) xs)
| Choice2Of2 xs -> xs |> List.sum
Choice1Of2 [| 1 .. 10 |] |> f |> printfn "%d"
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment