What does λ[α =>F] mean? [duplicate] - scalaz

This question already has an answer here:
Scalaz Functor typeclass special symbols
(1 answer)
Closed 7 years ago.
I'm learning Scalaz recently. I would like to know how λ[α =>F] works?
scala> Applicative[λ[α => Int]].point(10)
res45: Int = 0
scala> Applicative[λ[α => String]].point(10)
res46: String = ""
I can understand λ means some type here, but I could not find its definition and would like to know how the above code works.

scalaz use kind-projector.
Applicative[λ[α => Int]] is equivalent to Applicative[({type l[a] = Int})#l]
https://github.com/non/kind-projector
https://github.com/scalaz/scalaz/pull/875

Related

SQLALCHEMY: filter result if a value is in a list [duplicate]

This question already has answers here:
SQLAlchemy IN clause
(8 answers)
Closed 6 months ago.
I have a list of values and I want to filter this sqlalchemy query based on if a value is in that list. Something like this:
def get_values(db: Session=
l = [23, 34, 54]
qr = db.query(table).filter(table.column in l).all()
return qr
Thanks
You can use the in_ method on columns:
qr = db.query(table).filter(Model.column_id.in_(l)).all()
(Make sure you keep the underscore (_) at the end of in)

Is it possible in Go to call a function with named arguments? [duplicate]

This question already has answers here:
Initialize function fields
(2 answers)
Closed 3 years ago.
I want to call a function in Go, and attach to the argument values the argument names
func sum(a int, b int) int {
return a + b
}
func main() {
result := sum(a=4, b=5) // result == 9
}
Is it possible?
There is no such thing like named arguments in go
At the moment Go does not have a way to use named argument in functions.
If you really need to use named arguments you can try this library go-named-params

Is def x = 1 in Scala a variable declaration or a function? [duplicate]

This question already has answers here:
What is the difference between def foo = {} and def foo() = {} in Scala?
(4 answers)
Closed 7 years ago.
Is def x = 1 a function or a variable declaration? And, what is the difference between:
def x = 1 // REPL x: Int
def x() = 1 // REPL x: () Int
Looks like the first one is a variable definition. Please clarify.
No difference at all. Braces are optional for methods with no arguments in Scala. It is a convention to use them, if the method modifies any kind of state, and to leave the away, if it does not (at the call site too).
Both are method definitions. var x = 1 or val x = 1 would be variable definitions.

Scala "Sentence-Like" Function Definition [duplicate]

This question already has answers here:
When to use parenthesis in Scala infix notation
(4 answers)
Closed 7 years ago.
I am refactoring some code and wanted to learn how Scala methods are written so that they can be written like:
foo = Map("Hello" -> 1)
foo contains "Hello"
where "contains" is the style I am looking to emulate. Here is the code I am refactoring (from exercism.io):
class Bob {
def hey(statement:String): String = statement match {
case x if isSilent(x) => "Fine. Be that way!"
case x if shouting(x) => "Whoa, chill out!"
case x if asking(x) => "Sure."
case _ => "Whatever."
}
def isSilent2:String => Boolean = _.trim.isEmpty
def isSilent (str:String) = str.trim.isEmpty
def shouting(str:String): Boolean = str.toUpperCase == str && str.toLowerCase != str
def asking(str:String): Boolean = str.endsWith("?")
}
Ideally, I'd like to make my isSilent, shouting, and asking functions all able to be written in that style so that I can write:
case x if isSilent x => ...
Thanks for your help! Additionally, knowing what this is called in Scala (and other functional languages, because I think Haskell has something similar) would be really helpful as I've done quite a bit of searching and couldn't find what I was trying to describe.
This is referred to as Infix Notation and it doesn't require anything special so long as you have a single argument function.
The reason it isn't working for you is that you need the object whose method is being called. The following compiles:
class Bob {
def hey(statement:String): String = statement match {
case x if this isSilent x => "Fine. Be that way!"
case x if this shouting x => "Whoa, chill out!"
case x if this asking x => "Sure."
case _ => "Whatever."
}
def isSilent2:String => Boolean = _.trim.isEmpty
def isSilent (str:String) = str.trim.isEmpty
def shouting(str:String): Boolean = str.toUpperCase == str && str.toLowerCase != str
def asking(str:String): Boolean = str.endsWith("?")
}

How do I make lambda functions generic in Scala? [duplicate]

This question already has answers here:
How can I define an anonymous generic Scala function?
(2 answers)
Closed 9 years ago.
As most of you probably know you can define functions in 2 ways in scala, there's the 'def' method and the lambda method...
making the 'def' kind generic is fairly straight forward
def someFunc[T](a: T) { // insert body here
what I'm having trouble with here is how to make the following generic:
val someFunc = (a: Int) => // insert body here
of course right now a is an integer, but what would I need to do to make it generic?
val someFunc[T] = (a: T) => doesn't work, neither does val someFunc = [T](a: T) =>
Is it even possible to make them generic, or should I just stick to the 'def' variant?
As Randall Schulz said, def does not create a function, but a method. However, it can return a function and this way you can create generic functions like the identity function in Predef. This would look like this:
def myId[A] = (a: A) => a
List(1,2,3) map myId
// List(1,2,3)
List("foo") map myId
// List("foo")
But be aware, that calling myId without any type information infers Nothing. In the above case it works, because the type inference uses the signature of map, which is map[B](f: A => B) , where A is the type of the list and B gets infered to the same as A, because that is the signature of myId.
I don't believe it's possible. You can look at this previous post for more details:
How can I define an anonymous generic Scala function?
The only way around it (as one of the answers mentions) is to extend something like FunctionX and use a generic at the class level and then use that in the override of the apply function.
I don't believe it's possible either, but I'm a pessimist.
http://www.chuusai.com/2012/04/27/shapeless-polymorphic-function-values-1/
Edit:
Tell me if this isn't what you're asking, but this is why the accepted answer isn't what I thought you were asking for, see the link:
scala> :pa
// Entering paste mode (ctrl-D to finish)
def myId[A] = (a: A) => a
List(1,2,3) map myId
// List(1,2,3)
List("foo") map myId
// List("foo")
// Exiting paste mode, now interpreting.
myId: [A]=> A => A
res0: List[String] = List(foo)
scala> val f1 = myId[Int]
f1: Int => Int = <function1>
scala> val f2 = myId[String]
f2: String => String = <function1>
scala> List(1,2,3) map f2
<console>:10: error: type mismatch;
found : String => String
required: Int => ?
List(1,2,3) map f2
^
scala> List("foo") map f1
<console>:10: error: type mismatch;
found : Int => Int
required: String => ?
List("foo") map f1
^
The function values are not polymorphic, i.e., generic.
The closest thing is polymorphic functions I believe:
https://github.com/milessabin/shapeless#polymorphic-function-values