Coder Social home page Coder Social logo

dsc-enterprise-hsbc-scala-higher-order-functions's Introduction

Higher Order Functions

Higher Order Functions

A higher order function is a function or method that:

  • Takes other functions as parameters
  • Has a result as a function

A method that takes a function

def process(x:Int, y:Int, f:(Int, Int) => Int) = f(x,y)
process: (x: Int, y: Int, f: (Int, Int) => Int)Int

The above example has three arguments: An Int, another Int, and a higher order function that takes two Int and returns an Int

To run the above we can run:

process(4, 6, (x, y) => x * y)
res5: Int = 24

or

process(4, 6, _ * _)
res6: Int = 24

A function that takes a function

Of course a function can also take a function

val process = (x:Int, y:Int, f:(Int, Int) => Int) => f(x, y)
process: (Int, Int, (Int, Int) => Int) => Int = <function3>

The above example is a function that has three arguments: An Int, another Int, and a higher order function that takes two Int and returns an Int

To run the above we can run:

process(4, 6, (x, y) => x * y)
res7: Int = 24

or

process(4, 6, _ * _)
res8: Int = 24

A function that returns a function

  • A function, or method can return a function
  • This is also a higher order function
val process = (x:Int, y:Int) => (z:Int) => x + y + z
val f2 = process(10, 10)
f2(3)
process: (Int, Int) => Int => Int = <function2>
f2: Int => Int = <function1>
res9: Int = 23

A method returning a function, note the def

def process(x:Int, y:Int) = (z:Int) => x + y + z
val f2 = process(10, 10)
f2(3) //23
process: (x: Int, y: Int)Int => Int
f2: Int => Int = <function1>
res10: Int = 23

Running both a def and a function as if they are they’re the same semantics is no accident. Though mechanically different after a while the difference between the two becomes blurry.

Higher Order Functions Conclusion

  • Can come in either of these forms
    • Functions taking other functions
    • Functions returning other function
    • Methods taking functions
    • Methods returning functions
  • The distinguishing features of methods and functions becomes blurry

dsc-enterprise-hsbc-scala-higher-order-functions's People

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.