Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Cont monad in C#
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
// Cont monad in C#. //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public delegate TR Cont<TA, TR>(Func<TA, TR> c); public static class LinqContMonad { public static Cont<TU, TR> Select<TA, TR, TU>(this Cont<TA, TR> p, Func<TA, TU> selector) { return c => p(a => c(selector(a))); } public static Cont<TV, TR> SelectMany<TA, TR, TU, TV>( this Cont<TA, TR> p, Func<TA, Cont<TU, TR>> selector, Func<TA, TU, TV> projector) { return c => p(a => selector(a)(u => c(projector(a, u)))); } } public class Program { public static void Main(string[] args) { Cont<int, string> ex1 = from a in (Cont<int, string>)(c => c(1)) from b in (Cont<int, string>)(c => c(10)) select a + b; // result1 == "11" string result1 = ex1(x => x.ToString()); Console.WriteLine(result1); Cont<int, string> ex2 = from a in (Cont<int, string>)(c => c(1)) from b in (Cont<int, string>)(fred => "escape") select a + b; // result2 == "escape" string result2 = ex2(x => x.ToString()); Console.WriteLine(result2); Cont<int, string> ex3 = from a in (Cont<int, string>)(c => c(1)) from b in (Cont<int, string>)(fred => fred(10) + fred(20) ) select a + b; // result3 == "1121" string result3 = ex3(x => x.ToString()); Console.WriteLine(result3); } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0.26 sec, absolute running time: 0.11 sec, cpu time 0.09 sec, average memory usage: 14 Mb, average nr of threads: 4
fork mode
|
history
|
discussion