Skip to content

Instantly share code, notes, and snippets.

@tomphp
Last active October 13, 2023 12:03
Show Gist options
  • Save tomphp/9f2e4076de30cc6253e47e6daf95097e to your computer and use it in GitHub Desktop.
Save tomphp/9f2e4076de30cc6253e47e6daf95097e to your computer and use it in GitHub Desktop.
Generic Test
// Provided by Andrew Gibson @gooballLogic
// Set up:
// - dotnet new console
// - dotnet add package OneOf
// Implement each of the methods below so that they all compile
// There only one valid implementation for each function (apart from f5 & f6)
using OneOf;
A F1<A>(A a) {
/* TODO */
}
A F2<A, B>(A a, B b) {
/* TODO */
}
B F3<A, B>(A a, Func<A, B> a2b) {
/* TODO */
}
C F4<A, B, C>(Func<B, C> b2c, Func<A, B> a2b, A a) {
/* TODO */
}
// two possible solutions
A F5<A>(A? optionalA, A a) {
/* TODO */
}
// two possible solutions
OneOf<A, B> F6<A, B>(A? optionalA, B b) {
/* TODO */
}
C F7<A, B, C>(Func<A, B, C> ab2c, Func<A, B> a2b, A a) {
/* TODO */
}
B F8<A, B>(Func<A, B> a2b, Func<Func<A, B>, B> a2b2b) {
/* TODO */
}
C F9<A, B, C>(Func<A, B, C> ab2c, Func<Func<A, C>, C> a2c2c, B b) {
/* TODO */
}
-- Implement each of the functions below so that they all compile
-- There only one valid implementation for each function (apart from f5 & f6)
f1 :: a -> a
f1 = undefined
f2 :: a -> b -> a
f2 = undefined
f3 :: a -> (a -> b) -> b
f3 = undefined
f4 :: (b -> c) -> (a -> b) -> a -> c
f4 = undefined
-- two possible solutions
f5 :: Maybe a -> a -> a
f5 = undefined
-- two possible solutions
f6 :: Maybe a -> b -> Either b a
f6 = undefined
f7 :: (a -> b -> c) -> (a -> b) -> a -> c
f7 = undefined
f8 :: (a -> b) -> ((a -> b) -> b) -> b
f8 = undefined
f9 :: (a -> b -> c) -> ((a -> c) -> c) -> b -> c
f9 = undefined
// Implement each of the functions below so that they all compile
// There only one valid implementation for each function (apart from f5 & f6)
sealed class Optional<out A> {
data class Some<A>(val value: A): Optional<A>()
object None: Optional<Nothing>()
}
sealed class Either<out A, out B> {
data class Left<A>(val value: A): Either<A, Nothing>()
data class Right<B>(val value: B): Either<Nothing, B>()
}
fun <A>f1(a: A): A = /* TODO */
fun <A, B>f2(a: A, b: B): A = /* TODO */
fun <A, B>f3(a: A, a2b: (A) -> B): B = /* TODO */
fun <A, B, C>f4(b2c: (B) -> C, a2b: (A) -> B, a: A): C = /* TODO */
// two possible solutions
fun <A>f5(optionalA: Optional<A>, a: A): A = /* TODO */
// two possible solutions
fun <A, B>f6(optionalA: Optional<A>, b: B): Either<B, A> = /* TODO */
fun <A, B, C>f8(ab2c: (A, B) -> C, a2b: (A) -> B, a: A): C = /* TODO */
fun <A, B>f8(a2b: (A) -> B, a2b2b: ((A) -> B) -> B): B = /* TODO */
fun <A, B, C>f9(ab2c: (A, B) -> C, a2c2c: ((A) -> C) -> C, b: B) = /* TODO */
// Implement each of the functions below so that they all compile
// There only one valid implementation for each function (apart from f5 & f6)
function f1<A>(a: A): A {
/* TODO */
}
function f2<A, B>(a: A, b: B): A {
/* TODO */
}
function f3<A, B>(a: A, a2b: (A) => B): B {
/* TODO */
}
function f4<A, B, C>(b2c: (B) => C, a2b: (A) => B, a: A): C {
/* TODO */
}
// two possible solutions
function f5<A>(optionalA: A | undefined, a: A): A {
/* TODO */
}
// two possible solutions
function f6<A, B>(optionalA: A | undefined, b: B): A | B {
/* TODO */
}
function f7<A, B, C>(ab2c: (a: A, b: B) => C, a2b: (a: A) => B, a: A): A {
/* TODO */
}
function f8<A, B>(a2b: (a: A) => B, a2b2b: (a2b: (a: A) => B) => B): B {
/* TODO */
}
function f9<A, B, C>(ab2c: (a: A, b: B) => C, a2c2c: (a2c: (a: A) => C) => C, b: B): C {
/* TODO */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment