Skip to content

Instantly share code, notes, and snippets.

@benoitjadinon
Last active November 10, 2015 20:45
Show Gist options
  • Select an option

  • Save benoitjadinon/af3928fca10d2b2d1db7 to your computer and use it in GitHub Desktop.

Select an option

Save benoitjadinon/af3928fca10d2b2d1db7 to your computer and use it in GitHub Desktop.
functional port exercise, based on the provided Kotlin sample
//https://dotnetfiddle.net/KXXMef
// shorter but less generic :
// https://dotnetfiddle.net/R3m1h2
using System;
using System.Collections;
using System.Linq;
public class Program
{
public static void Main()
{
new Program().mainNonStatic();
}
public void mainNonStatic()
{
var oddLength = compose (isOdd, length);
var strings = new []{ "a", "ab", "abc" };
Console.WriteLine(string.Join(", ", strings.Where (oddLength).ToArray()));
}
Func<int, bool> isOdd = (x) => x % 2 != 0;
Func<string, int> length = (x) => x.Length;
Func<A, C> compose<A,B,C>(Func<B, C> f, Func<A, B> g){
return x => f(g(x));
}
}
// https://dotnetfiddle.net/Eh8qh5
let isOdd x = (x % 2) <> 0
let length (s:string) = s.Length
let compose (f : ('b -> 'c)) (g : ('a -> 'b)) : ('a -> 'c) = fun x -> f(g(x));
// or better:
//let compose len odd s = s |> len |> odd
[<EntryPoint>]
let main args =
let oddLength = compose isOdd length
let strings = [|"a"; "ab"; "abc"|];
printfn "%A" (strings |> Array.filter (oddLength))
0
// http://ideone.com/QYmgpw
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
OddFunc isOdd = (x) -> x % 2 != 0;
LenFunc length = (x) -> { return x.length(); };
Predicate<String> oddLength = compose(isOdd, length);
List<String> strings = Arrays.asList("a", "ab", "abc");
String stringsFiltered = strings.stream().filter(oddLength).collect(Collectors.joining(", "));
System.out.println(stringsFiltered);
}
static Predicate<String> compose(OddFunc f, LenFunc g)
{
return (x) -> f.operation(g.operation(x));
}
interface OddFunc
{
Boolean operation(Integer a);
}
interface LenFunc
{
Integer operation(String a);
}
}
// http://try.kotlinlang.org/#/Examples/Callable%20references/Composition%20of%20functions/Composition%20of%20functions.kt
fun main(args: Array<String>) {
val oddLength = compose(::isOdd, ::length)
val strings = listOf("a", "ab", "abc")
println(strings.filter(oddLength))
}
fun isOdd(x: Int) = x % 2 != 0
fun length(s: String) = s.length
fun <A, B, C> compose(f: (B) -> C, g: (A) -> B): (A) -> C {
return { x -> f(g(x)) }
}
// http://swiftstub.com/374317108
//func isOdd (x: Int) -> Bool { return x % 2 != 0 } // can be infered to:
let isOdd = { return $0 % 2 != 0 };
func length (s: String) -> Int { return s.characters.count; }
func compose<A, B, C> (f: (B) -> C, _ g: (A) -> B) -> (x:A) -> C {
func ret (x:A) -> C {
return f(g(x));
};
return ret;
}
let oddLength = compose(isOdd, length)
let strings = ["a", "ab", "abc"]
print(strings.filter(oddLength))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment