Skip to content

Instantly share code, notes, and snippets.

@bangarharshit
Created April 9, 2024 10:42
Show Gist options
  • Save bangarharshit/0dc8c32060f4a9944e96d167dda26ee2 to your computer and use it in GitHub Desktop.
Save bangarharshit/0dc8c32060f4a9944e96d167dda26ee2 to your computer and use it in GitHub Desktop.
import UIKit
// Runnable -> no input, no output
// Callable -> no input -> Producer
// Function -> input, output
// Consumer
// **Higher order function**
func runnable() {
print("this is some lambda")
}
func consumer(a: String) {
print(a)
}
func producer() -> String {
return "a"
}
func producer2() -> String {
return "b"
}
func functional(a: String) -> String {
return a.uppercased()
}
func anotherFunction(randomFunction:()->String) {
print(randomFunction())
}
func newFunction(r: (String)-> String) { // r is functional
print(r("Gello1")) // print(functional("hello"))
}
func ViewCreate(clickHandler:()->()) {
// framework view clicked
clickHandler()
}
anotherFunction(randomFunction: producer)
newFunction(r: functional)
// Lambda - only this is required
newFunction { a in
return a.lowercased()
}
//anotherFunction(randomFunction: producer2)
func unit_test(setup:()->(), teardown:()->()) {
setup()
// unit test code
teardown()
}
let f1: ()->() = runnable
let f2: (String) -> () = consumer
let f3: ()-> (String) = producer
let f4: (String)->(String) = functional
let f5 = producer // type inference
// f1...f5-> storing function in a variable
// int, function
print(f3()) // function call
print(f5()) // print(producer())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment