Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
func ( : ) ->
(( ) -> )
func ( : ) ->
( ) ->
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
func add(_ value1: Int, _ value2: Int) -> Int {
return value1 + value2
}
add(2,3)
func curriedAdd(_ value1: Int) -> ( (Int) -> Int ) {
return { value2 in
return value1 + value2
}
}
func curriedAdd(_ value1: Int) -> ( (Int) -> Int ) {
return { value2 in
return value1 + value2
}
}
let add2 = curriedAdd(2)
add2(3)
func curriedAdd(_ value1: Int) -> ( (Int) -> Int ) {
return { value2 in
return value1 + value2
}
}
let add2 = curriedAdd(2)
add2(3)
curriedAdd(2)(3)
let add2 = curriedAdd(2)
add2(3)
curriedAdd(2)(3)
func curriedAdd(_ value1: Int) -> (Int) -> Int {
return { value2 in
return value1 + value2
}
}
Swift 함수 커링 사용하기
"aaa/bbb/ccc/ddd".components(separatedBy: "/")
"aaa*bbb*ccc*ddd".components(separatedBy: "*")
func stringDevider(_ seperater: String) -> (String) -> [String] {
return { (string: String) -> [String] in
return string.components(separatedBy: seperater)
}
}
let stringDevideBySlash = stringDevider("/")
let stringDevideByAsterisk = stringDevider("*")
stringDevideBySlash("aaa/bbb/ccc/ddd")
stringDevideByAsterisk("aaa*bbb*ccc*ddd")
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
class SomeClass {
func someFunction() {
}
}
class SomeClass {
func someFunction() {
}
}
let someinstance = SomeClass()
someinstance.someFunction()
class SomeClass {
func someFunction() {
}
}
let someinstance = SomeClass()
someinstance.someFunction()
class SomeClass {
func someFunction() {
}
}
let someinstance = SomeClass()
someinstance.someFunction()
class SomeClass {
func someFunction() {
}
}
let someinstance = SomeClass()
someinstance.someFunction()
class SomeClass {
func someFunction() {
}
}
let someinstance = SomeClass()
someinstance.someFunction()
SomeClass.someFunction(someinstance)()
class SomeClass {
func someFunction() {
}
}
let someinstance = SomeClass()
someinstance.someFunction()
SomeClass.someFunction(someinstance)()
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
func regexTest(pattern: String) -> (String) -> Bool
{
let expression: NSRegularExpression? =
try? NSRegularExpression(pattern: pattern,
options: .caseInsensitive)
return { (input: String) -> Bool in
guard let expression = expression else { return false }
let inputRange = NSMakeRange(0, input.characters.count)
let matches = expression.matches(in: input,
options: [],
range: inputRange)
return matches.count > 0
}
}
func regexTest(pattern: String) -> (String) -> Bool
{
let expression: NSRegularExpression? =
try? NSRegularExpression(pattern: pattern,
options: .caseInsensitive)
return { (input: String) -> Bool in
guard let expression = expression else { return false }
let inputRange = NSMakeRange(0, input.characters.count)
let matches = expression.matches(in: input,
options: [],
range: inputRange)
return matches.count > 0
}
}
func regexTest(pattern: String) -> (String) -> Bool
regexTest(pattern: "main")("int main()")
{
let expression: NSRegularExpression? =
try? NSRegularExpression(pattern: pattern,
options: .caseInsensitive)
return { (input: String) -> Bool in
guard let expression = expression else { return false }
let inputRange = NSMakeRange(0, input.characters.count)
let matches = expression.matches(in: input,
options: [],
range: inputRange)
return matches.count > 0
}
}
func regexTest(pattern: String) -> (String) -> Bool
regexTest(pattern: "main")("int main()")
let hasMainIn = regexTest(pattern: "main")
hasMainIn("int main()")
Swift 함수 커링 사용하기
class Regex {
var internalExpression: NSRegularExpression?
init(_ pattern: String) {
self.internalExpression =
try? NSRegularExpression(pattern: pattern, options: .caseInsensitive)
}
func test(_ input: String) -> Bool {
let inputRange = NSMakeRange(0, input.characters.count)
guard let matchesCount = self.internalExpression?
.matches(in: input, options: [], range:inputRange)
.count else { return false }
return matchesCount > 0
}
}
class Regex {
var internalExpression: NSRegularExpression?
init(_ pattern: String) {
self.internalExpression =
try? NSRegularExpression(pattern: pattern, options: .caseInsensitive)
}
func test(_ input: String) -> Bool {
let inputRange = NSMakeRange(0, input.characters.count)
guard let matchesCount = self.internalExpression?
.matches(in: input, options: [], range:inputRange)
.count else { return false }
return matchesCount > 0
}
}
let regex = Regex("main")
regex.test("int main()")
Regex("main").test("int main()")
class Regex {
var internalExpression: NSRegularExpression?
init(_ pattern: String) {
self.internalExpression =
try? NSRegularExpression(pattern: pattern, options: .caseInsensitive)
}
func test(_ input: String) -> Bool {
let inputRange = NSMakeRange(0, input.characters.count)
guard let matchesCount = self.internalExpression?
.matches(in: input, options: [], range:inputRange)
.count else { return false }
return matchesCount > 0
}
}
let regex = Regex("main")
regex.test("int main()")
Regex("main").test("int main()")
let hasMainIn = Regex("main").test
hasMainIn("int main()")
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
struct Friend {
var name: String
var id: Int
}
struct Friend {
var name: String
var id: Int
}
let friends = [Friend(name:" ", id: 1),
Friend(name:" ", id: 2),
Friend(name:" ", id: 3),
Friend(name:" ", id: 4),
Friend(name:" ", id: 5)]
struct Friend {
var name: String
var id: Int
}
let friends = [Friend(name:" ", id: 1),
Friend(name:" ", id: 2),
Friend(name:" ", id: 3),
Friend(name:" ", id: 4),
Friend(name:" ", id: 5)]
func inviteFriend(ID: Int) -> Void
let actionsheet = UIAlertController(title: " ", message: " ",
preferredStyle: .actionSheet)
let actionsheet = UIAlertController(title: " ", message: " ",
preferredStyle: .actionSheet)
UIAlertAction(title: String?, style: UIAlertActionStyle, handler:
((UIAlertAction) -> Void)?)
func actionHandler(friendID: Int) -> (UIAlertAction) -> () {
return { [weak self] action -> Void in
self?.inviteFriend(ID: friendID)
}
}
friends.forEach{ (friend: Friend) in
actionsheet.addAction(UIAlertAction(title: friend.name,
style: UIAlertActionStyle.default,
handler: actionHandler(friendID:
friend.id)))
}
actionsheet.addAction(UIAlertAction(title: " ", style: .cancel, handler: nil))
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
class ViewController: UIViewController {
@IBOutlet var yellowBox: UIView!
}
class ViewController: UIViewController {
@IBOutlet var yellowBox: UIView!
}
extension ViewController {
@IBAction func leftButtonTapped(_ sender: Any) {
UIView.animate(withDuration: 0.5, animations: {
var frame = self.yellowBox.frame
frame.origin.x = frame.origin.x - 100
self.yellowBox.frame = frame
}, completion: nil)
}
@IBAction func rightButtonTapped(_ sender: Any) {
UIView.animate(withDuration: 0.5, animations: {
var frame = self.yellowBox.frame
frame.origin.x = frame.origin.x + 100
self.yellowBox.frame = frame
}, completion: nil)
}
@IBAction func upButtonTapped(_ sender: Any) {
UIView.animate(withDuration: 0.5, animations: {
var frame = self.yellowBox.frame
frame.origin.y = frame.origin.y - 100
extension ViewController {
@IBAction func leftButtonTapped(_ sender: Any) {
UIView.animate(withDuration: 0.5, animations: {
var frame = self.yellowBox.frame
frame.origin.x = frame.origin.x - 100
self.yellowBox.frame = frame
}, completion: nil)
}
@IBAction func rightButtonTapped(_ sender: Any) {
UIView.animate(withDuration: 0.5, animations: {
var frame = self.yellowBox.frame
frame.origin.x = frame.origin.x + 100
self.yellowBox.frame = frame
}, completion: nil)
}
@IBAction func upButtonTapped(_ sender: Any) {
UIView.animate(withDuration: 0.5, animations: {
var frame = self.yellowBox.frame
frame.origin.y = frame.origin.y - 100
self.yellowBox.frame = frame
}, completion: nil)
}
@IBAction func downButtonTapped(_ sender: Any) {
UIView.animate(withDuration: 0.5, animations: {
var frame = self.yellowBox.frame
frame.origin.y = frame.origin.y + 100
self.yellowBox.frame = frame
}, completion: nil)
}
}
enum AnimationDirection {
case up
case down
case left
case right
}
extension ViewController {
func moveBox(to: AnimationDirection) -> () -> Void {
return {
var frame = self.yellowBox.frame
switch to {
case .left:
frame.origin.x = frame.origin.x - 100
case .right:
frame.origin.x = frame.origin.x + 100
case .down:
frame.origin.y = frame.origin.y + 100
case .up:
frame.origin.y = frame.origin.y - 100
}
self.yellowBox.frame = frame
}
}
@IBAction func leftButtonTapped(_ sender: Any) {
frame.origin.x = frame.origin.x + 100
case .down:
frame.origin.y = frame.origin.y + 100
case .up:
frame.origin.y = frame.origin.y - 100
}
self.yellowBox.frame = frame
}
}
@IBAction func leftButtonTapped(_ sender: Any) {
UIView.animate(withDuration: 0.5,
animations: moveBox(to: .left),
completion: nil)
}
@IBAction func rightButtonTapped(_ sender: Any) {
UIView.animate(withDuration: 0.5,
animations: moveBox(to: .right),
completion: nil)
}
@IBAction func upButtonTapped(_ sender: Any) {
UIView.animate(withDuration: 0.5,
animations: moveBox(to: .up),
completion: nil)
}
@IBAction func downButtonTapped(_ sender: Any) {
UIView.animate(withDuration: 0.5,
animations: moveBox(to: .down),
completion: nil)
}
}
Swift 함수 커링 사용하기
viewController.api = { (pageID: Int) -> Observable<[Post]> in
// alamofire api Observable Return
}
viewController.api = { (pageID: Int) -> Observable<[Post]> in
// alamofire api Observable Return
}
func searchAPI(searchText: String) -> (Int) -> Observable<[Post]>
self.api = searchAPI(searchText: "search keywords")
func curry<A, B, C>(f: @escaping (A, B) -> C) -> ((A) -> ((B) -> C)) {
return { x in { y in f(x, y) }}
}
func fiveTimeCurry<A, B, C, D, E>(_ a: A) -> (B) -> (C) -> (D) -> E {
}
Swift 함수 커링 사용하기
g(f(x))
g(f(x))
let result = bind(add2, 3) // result => 5
g(f(x))
let result = bind(add2, 3) // result => 5
infix operator >>=: MultiplicationPrecedence
func >>=<A, B>(a: A, f: (A) -> B) -> B
?

More Related Content

Swift 함수 커링 사용하기

  • 12. func ( : ) -> (( ) -> )
  • 13. func ( : ) -> ( ) ->
  • 18. func add(_ value1: Int, _ value2: Int) -> Int { return value1 + value2 } add(2,3)
  • 19. func curriedAdd(_ value1: Int) -> ( (Int) -> Int ) { return { value2 in return value1 + value2 } }
  • 20. func curriedAdd(_ value1: Int) -> ( (Int) -> Int ) { return { value2 in return value1 + value2 } } let add2 = curriedAdd(2) add2(3)
  • 21. func curriedAdd(_ value1: Int) -> ( (Int) -> Int ) { return { value2 in return value1 + value2 } } let add2 = curriedAdd(2) add2(3) curriedAdd(2)(3)
  • 22. let add2 = curriedAdd(2) add2(3) curriedAdd(2)(3) func curriedAdd(_ value1: Int) -> (Int) -> Int { return { value2 in return value1 + value2 } }
  • 25. func stringDevider(_ seperater: String) -> (String) -> [String] { return { (string: String) -> [String] in return string.components(separatedBy: seperater) } } let stringDevideBySlash = stringDevider("/") let stringDevideByAsterisk = stringDevider("*") stringDevideBySlash("aaa/bbb/ccc/ddd") stringDevideByAsterisk("aaa*bbb*ccc*ddd")
  • 28. class SomeClass { func someFunction() { } }
  • 29. class SomeClass { func someFunction() { } } let someinstance = SomeClass() someinstance.someFunction()
  • 30. class SomeClass { func someFunction() { } } let someinstance = SomeClass() someinstance.someFunction()
  • 31. class SomeClass { func someFunction() { } } let someinstance = SomeClass() someinstance.someFunction()
  • 32. class SomeClass { func someFunction() { } } let someinstance = SomeClass() someinstance.someFunction()
  • 33. class SomeClass { func someFunction() { } } let someinstance = SomeClass() someinstance.someFunction() SomeClass.someFunction(someinstance)()
  • 34. class SomeClass { func someFunction() { } } let someinstance = SomeClass() someinstance.someFunction() SomeClass.someFunction(someinstance)()
  • 40. func regexTest(pattern: String) -> (String) -> Bool
  • 41. { let expression: NSRegularExpression? = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) return { (input: String) -> Bool in guard let expression = expression else { return false } let inputRange = NSMakeRange(0, input.characters.count) let matches = expression.matches(in: input, options: [], range: inputRange) return matches.count > 0 } } func regexTest(pattern: String) -> (String) -> Bool
  • 42. { let expression: NSRegularExpression? = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) return { (input: String) -> Bool in guard let expression = expression else { return false } let inputRange = NSMakeRange(0, input.characters.count) let matches = expression.matches(in: input, options: [], range: inputRange) return matches.count > 0 } } func regexTest(pattern: String) -> (String) -> Bool regexTest(pattern: "main")("int main()")
  • 43. { let expression: NSRegularExpression? = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) return { (input: String) -> Bool in guard let expression = expression else { return false } let inputRange = NSMakeRange(0, input.characters.count) let matches = expression.matches(in: input, options: [], range: inputRange) return matches.count > 0 } } func regexTest(pattern: String) -> (String) -> Bool regexTest(pattern: "main")("int main()") let hasMainIn = regexTest(pattern: "main") hasMainIn("int main()")
  • 45. class Regex { var internalExpression: NSRegularExpression? init(_ pattern: String) { self.internalExpression = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) } func test(_ input: String) -> Bool { let inputRange = NSMakeRange(0, input.characters.count) guard let matchesCount = self.internalExpression? .matches(in: input, options: [], range:inputRange) .count else { return false } return matchesCount > 0 } }
  • 46. class Regex { var internalExpression: NSRegularExpression? init(_ pattern: String) { self.internalExpression = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) } func test(_ input: String) -> Bool { let inputRange = NSMakeRange(0, input.characters.count) guard let matchesCount = self.internalExpression? .matches(in: input, options: [], range:inputRange) .count else { return false } return matchesCount > 0 } } let regex = Regex("main") regex.test("int main()") Regex("main").test("int main()")
  • 47. class Regex { var internalExpression: NSRegularExpression? init(_ pattern: String) { self.internalExpression = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) } func test(_ input: String) -> Bool { let inputRange = NSMakeRange(0, input.characters.count) guard let matchesCount = self.internalExpression? .matches(in: input, options: [], range:inputRange) .count else { return false } return matchesCount > 0 } } let regex = Regex("main") regex.test("int main()") Regex("main").test("int main()") let hasMainIn = Regex("main").test hasMainIn("int main()")
  • 53. struct Friend { var name: String var id: Int }
  • 54. struct Friend { var name: String var id: Int } let friends = [Friend(name:" ", id: 1), Friend(name:" ", id: 2), Friend(name:" ", id: 3), Friend(name:" ", id: 4), Friend(name:" ", id: 5)]
  • 55. struct Friend { var name: String var id: Int } let friends = [Friend(name:" ", id: 1), Friend(name:" ", id: 2), Friend(name:" ", id: 3), Friend(name:" ", id: 4), Friend(name:" ", id: 5)] func inviteFriend(ID: Int) -> Void
  • 56. let actionsheet = UIAlertController(title: " ", message: " ", preferredStyle: .actionSheet)
  • 57. let actionsheet = UIAlertController(title: " ", message: " ", preferredStyle: .actionSheet) UIAlertAction(title: String?, style: UIAlertActionStyle, handler: ((UIAlertAction) -> Void)?)
  • 58. func actionHandler(friendID: Int) -> (UIAlertAction) -> () { return { [weak self] action -> Void in self?.inviteFriend(ID: friendID) } } friends.forEach{ (friend: Friend) in actionsheet.addAction(UIAlertAction(title: friend.name, style: UIAlertActionStyle.default, handler: actionHandler(friendID: friend.id))) } actionsheet.addAction(UIAlertAction(title: " ", style: .cancel, handler: nil))
  • 67. class ViewController: UIViewController { @IBOutlet var yellowBox: UIView! }
  • 68. class ViewController: UIViewController { @IBOutlet var yellowBox: UIView! } extension ViewController { @IBAction func leftButtonTapped(_ sender: Any) { UIView.animate(withDuration: 0.5, animations: { var frame = self.yellowBox.frame frame.origin.x = frame.origin.x - 100 self.yellowBox.frame = frame }, completion: nil) } @IBAction func rightButtonTapped(_ sender: Any) { UIView.animate(withDuration: 0.5, animations: { var frame = self.yellowBox.frame frame.origin.x = frame.origin.x + 100 self.yellowBox.frame = frame }, completion: nil) } @IBAction func upButtonTapped(_ sender: Any) { UIView.animate(withDuration: 0.5, animations: { var frame = self.yellowBox.frame frame.origin.y = frame.origin.y - 100
  • 69. extension ViewController { @IBAction func leftButtonTapped(_ sender: Any) { UIView.animate(withDuration: 0.5, animations: { var frame = self.yellowBox.frame frame.origin.x = frame.origin.x - 100 self.yellowBox.frame = frame }, completion: nil) } @IBAction func rightButtonTapped(_ sender: Any) { UIView.animate(withDuration: 0.5, animations: { var frame = self.yellowBox.frame frame.origin.x = frame.origin.x + 100 self.yellowBox.frame = frame }, completion: nil) } @IBAction func upButtonTapped(_ sender: Any) { UIView.animate(withDuration: 0.5, animations: { var frame = self.yellowBox.frame frame.origin.y = frame.origin.y - 100 self.yellowBox.frame = frame }, completion: nil) } @IBAction func downButtonTapped(_ sender: Any) { UIView.animate(withDuration: 0.5, animations: { var frame = self.yellowBox.frame frame.origin.y = frame.origin.y + 100 self.yellowBox.frame = frame }, completion: nil) } }
  • 70. enum AnimationDirection { case up case down case left case right } extension ViewController { func moveBox(to: AnimationDirection) -> () -> Void { return { var frame = self.yellowBox.frame switch to { case .left: frame.origin.x = frame.origin.x - 100 case .right: frame.origin.x = frame.origin.x + 100 case .down: frame.origin.y = frame.origin.y + 100 case .up: frame.origin.y = frame.origin.y - 100 } self.yellowBox.frame = frame } } @IBAction func leftButtonTapped(_ sender: Any) {
  • 71. frame.origin.x = frame.origin.x + 100 case .down: frame.origin.y = frame.origin.y + 100 case .up: frame.origin.y = frame.origin.y - 100 } self.yellowBox.frame = frame } } @IBAction func leftButtonTapped(_ sender: Any) { UIView.animate(withDuration: 0.5, animations: moveBox(to: .left), completion: nil) } @IBAction func rightButtonTapped(_ sender: Any) { UIView.animate(withDuration: 0.5, animations: moveBox(to: .right), completion: nil) } @IBAction func upButtonTapped(_ sender: Any) { UIView.animate(withDuration: 0.5, animations: moveBox(to: .up), completion: nil) } @IBAction func downButtonTapped(_ sender: Any) { UIView.animate(withDuration: 0.5, animations: moveBox(to: .down), completion: nil) } }
  • 73. viewController.api = { (pageID: Int) -> Observable<[Post]> in // alamofire api Observable Return }
  • 74. viewController.api = { (pageID: Int) -> Observable<[Post]> in // alamofire api Observable Return } func searchAPI(searchText: String) -> (Int) -> Observable<[Post]> self.api = searchAPI(searchText: "search keywords")
  • 75. func curry<A, B, C>(f: @escaping (A, B) -> C) -> ((A) -> ((B) -> C)) { return { x in { y in f(x, y) }} } func fiveTimeCurry<A, B, C, D, E>(_ a: A) -> (B) -> (C) -> (D) -> E { }
  • 78. g(f(x)) let result = bind(add2, 3) // result => 5
  • 79. g(f(x)) let result = bind(add2, 3) // result => 5 infix operator >>=: MultiplicationPrecedence func >>=<A, B>(a: A, f: (A) -> B) -> B
  • 80. ?