How to call a Scala function with a single parameter and write the parameter first

lex82

I have some code that converts the elements of sequences with different functions like that:

someSequence map converterFunction

However, sometimes I have not a sequence but a single value that is to be passed to the function. For consistency with the other lines I'd like to write it like that

someValue applyTo converterFunction

So in a way it is like mapping a single value. Of course I can just call the function with the value, I'm just wondering if it is possible to write it similar to the way I proposed.

SergGr

I agree with ChrisK that this idea doesn't sound as a good one to me in terms of code readability but if you really want it, you can do it using something like this:

implicit final class MapAnyOp[T](val value: T) extends AnyVal {
  def map[R](f: T => R) = f(value)
}

def convert(v: Int): String = Integer.toHexString(v)

println(List(123, 234, 345) map convert)
println(123 map convert)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to call a function on an object with a generic type parameter using scala macros?

From Dev

How to call a function with a function as parameter?

From Dev

Is it possible in Scala to write an expression that placed in a function parameter, defaults the parameter value?

From Dev

Scala function call with unknown default parameter

From Dev

LINQ First() or Single() parameter

From Dev

How to write alias to use first parameter or * if parameter is blank?

From Dev

Why can I call += with a single parameter on a Scala Set?

From Dev

How to write an Action function with two parameter in java

From Dev

Scala: How to define anonymous function with implicit parameter?

From Dev

How to initialize function type parameter in Scala class?

From Dev

How to bind second parameter in scala curry function?

From Dev

How to access to the parameter of the higher order function in scala

From Dev

Write function with type parameter

From Dev

In Scala, is a function that takes call by name parameter different from a function that takes another function as parameter?

From Dev

In Scala, is a function that takes call by name parameter different from a function that takes another function as parameter?

From Dev

Scala function => as parameter

From Dev

Scala: function as a parameter to map()

From Dev

How to call a JavaScript function with parameter in jQuery?

From Dev

How to call one function with different parameter types

From Dev

How to call a JavaScript function with parameter in jQuery?

From Dev

How to call class template function with parameter

From Dev

How to call a function with a parameter as string in php?

From Dev

How to call DLL function using "&" parameter?

From Dev

How can I call function with parameter in bash?

From Dev

How to properly call a function with date parameter?

From Dev

how to call function where parameter based on a promise

From Dev

How to call on init function from textarea with parameter?

From Dev

How to call a parameter list from a function in Julia

From Dev

Function call as parameter value?

Related Related

  1. 1

    How to call a function on an object with a generic type parameter using scala macros?

  2. 2

    How to call a function with a function as parameter?

  3. 3

    Is it possible in Scala to write an expression that placed in a function parameter, defaults the parameter value?

  4. 4

    Scala function call with unknown default parameter

  5. 5

    LINQ First() or Single() parameter

  6. 6

    How to write alias to use first parameter or * if parameter is blank?

  7. 7

    Why can I call += with a single parameter on a Scala Set?

  8. 8

    How to write an Action function with two parameter in java

  9. 9

    Scala: How to define anonymous function with implicit parameter?

  10. 10

    How to initialize function type parameter in Scala class?

  11. 11

    How to bind second parameter in scala curry function?

  12. 12

    How to access to the parameter of the higher order function in scala

  13. 13

    Write function with type parameter

  14. 14

    In Scala, is a function that takes call by name parameter different from a function that takes another function as parameter?

  15. 15

    In Scala, is a function that takes call by name parameter different from a function that takes another function as parameter?

  16. 16

    Scala function => as parameter

  17. 17

    Scala: function as a parameter to map()

  18. 18

    How to call a JavaScript function with parameter in jQuery?

  19. 19

    How to call one function with different parameter types

  20. 20

    How to call a JavaScript function with parameter in jQuery?

  21. 21

    How to call class template function with parameter

  22. 22

    How to call a function with a parameter as string in php?

  23. 23

    How to call DLL function using "&" parameter?

  24. 24

    How can I call function with parameter in bash?

  25. 25

    How to properly call a function with date parameter?

  26. 26

    how to call function where parameter based on a promise

  27. 27

    How to call on init function from textarea with parameter?

  28. 28

    How to call a parameter list from a function in Julia

  29. 29

    Function call as parameter value?

HotTag

Archive