SyntaxError: invalid syntax [in python code] - ipython

code is:
cat_list = [k for k, v in cat_counter.()[:50]]
Error is as follows:
File "", line 1
cat_list = [k for k, v in cat_counter.()[:50]]
SyntaxError: invalid syntax

The cat_counter function would be defined like this:
def cat_counter():
# Make function
Thus, simply remove the dot to properly call the function:
cat_list = [k for k, v in cat_counter()[:50]]

Related

Calling Method inside a method giving a type mismatch error Scala

So I'm trying to call a method("Mean") from inside another method("Centre"), it's giving a type mismatch error.
But if I execute the method("Mean") seperately and store it's result in some variable and then execute method("Centre") using the variable instead of method("Mean"), it works.
Can anyone please explain why?
val X = Vector(3.0,4,5)
val Y = Vector(6,9.0,15)
type D = Double
type V = Vector[D]
def Mean (v:V)= v.sum/v.length
val meanX = Mean(X)
def Centre (v:V) = v.map(X => X - Mean(X))
Centre(X)
Error:
command-1723108043672149:8: error: type mismatch;
found : D
(which expands to) Double
required: V
(which expands to) scala.collection.immutable.Vector[Double]
def Centre (v:V) = v.map(X => X - Mean(X))
but it works if I use "meanX" instead of "Mean(X):
Centre: (v: V)scala.collection.immutable.Vector[Double]
res36: scala.collection.immutable.Vector[Double] = Vector(-1.0, 0.0,1.0)
Mean() is defined to take an argument of type V (i.e. Vector[Double]) but in this code, v.map(X => X - Mean(X)) you're trying to pass a Double value instead, because you have redefined the variable X. Thus the error.
I think what you want to do is v.map(n => n - Mean(X)) or, better yet, v.map(_ - Mean(X)). That way X has only one meaning.

How to implement twice function (function that executes other function twice)? Could not match type Record with type Function Int

Here is the code
module Main where
import Prelude
twice1 f = f . f
transform :: Int -> Int
transform n = n + 1
apply1 x = (twice1 transform) x
I have an error
Could not match type
Record
with type
Function Int
What's wrong? (you can try code here http://try.purescript.org)
PureScript uses the dot . for accessing record fields, as in:
r = { a: 42, b: "what?!" }
fourtyTwo = r.a
The function composition operator in PureScript is <<< (or >>> for left-to-right composition), for example:
twice1 f = f <<< f

How to unpack first-class module to module for object with class declaration?

I have:
module type A = sig end
This code is valid:
let f x =
let module X = (val x : A) in
object end
and this:
let f (module X : A) =
object end
But this is invalid:
class c x =
let module X = (val x : A) in
(* ^^^ Error: Syntax error *)
object end
and this:
class c (module X : A) =
(* ^^^^^^^^^^^^^ Error: Modules are not allowed in this pattern. *)
object end
And I can not understand: Why?
How to unpack first-class module to module for object in class context?
My compiler version = 4.01.0
Thank you
Any form of local module for a class definition is not allowed:
class c =
let module L = List in
object end;;
Characters 18-24:
let module L = List in
^^^^^^
Error: Syntax error
I'm not certain of the exact reason, but I have it on very good authority that it would be very difficult to implement.
However, it is possible to accept a first-class module argument to a class and unpack it within the methods and values that actually use it. For example:
class c d = object
method foo =
let module D = (val d : A) in
D.b + 5
end

list comprehension without parentheses in a function

I have a function that needs to return the last property of an object that satisfies the condition:
types = {
a: 1,
b: 2,
c: 3
}
g = (s) -> v for k, v of types when k is s
console.log g 'b'
this code prints [ 2 ]
I expected just 2, and not an array. And indeed, this code does print what I expect:
console.log v for k, v of types when k is 'b'
What is wrong?
P.S. I know that instead of this function I can just access the object's property using [], but this is a contrived example.
If we rearrange the code then things should be clearer.
Your second piece of code:
console.log v for k, v of types when k is 'b'
is just another way of writing this:
for k, v of types when k is 'b'
console.log(v)
or even:
for k, v of types
if k is 'b'
console.log(v)
Since there is only one 'b' key, only one console.log call is made.
Your first piece of code:
g = (s) -> v for k, v of types when k is s
is the same as this:
g = (s) ->
a = (v for k, v of types when k is s)
a
The loop, v for k, v of types when k is s yields an array by definition so a will be an array (with only one element) and g will return an array.
console.log v for k, v of types when k is 'b' will call console.log(v) for every v when k satisfies the condition whereas your first code snipped will call console.log(g(b)). If there were two elements in types that satisfied the condition, the outputs would be:
[1, 2]
and
1
2
To make g output the first element that satisfies the condition, you could use return with early out or just take the first element of the results array.
g = (s) -> return v for k, v of types when k is s

Using each right to pass parameters to a kdb function

I have the following code that does not work:
fun { [h; d]
h(anotherFun; d)
}
h: hopen(`hparam)
d: (2013.06.01, 2013.06.02)
h,/:fun d
What do I need to do to call fun with h and each element of d? The diagnostic from q is cryptic to say the least.
it seems like h and elements of d are your two arguments so you can try
h fun/: d
Easiest would be create projection with constant argument and just call it for each argument from the list
fun[h;] each d