I defined my model to be Model = Dict Int String however in compile time, I get number instead of Int so it is wrong:
The 2nd argument to function `get` is causing a mismatch.
71| Dict.get 3 model
^^^^^
Function `get` is expecting the 2nd argument to be:
Dict number v
But it is:
Model
and unfortunately Elm repl does the same thing returning Dict number instead of Dict Int.
> Dict.fromList [ (1, {a= 1} )]
Dict.fromList [(1,{ a = 1 })] : Dict.Dict number { a : number1 }
Certain languages such as Haskell expose Int as as well as Integer as well as number How can I coerce it to be integer?
Would you be able to provide the relevant code?
The following compiles and works fine for me:
import Dict exposing (Dict, fromList, get)
type alias Model = Dict Int String
model : Model
model = fromList [(1, "apple"), (2, "banana"), (42, "giraffe")]
test : Maybe String
test = get 2 model
Related
I'm trying to practice Haskell returns and datatypes. I'm trying to pass the following information into the program:
worm = 1:2:3:worm
eel = [1,2,3,1,2,3,1,2,3]
snake = 3:2:1:snake
whale = [1..100]
And i want to create a function that has a switch function to get the data and match it to its definition. For example, in Python:
def compare(str): #for one case and using string to clarify
if str == "1:2:3:worm":
return "worm"
I know the datatypes are lists but causes a lot of confusion. My code is giving me an error of Could not deduce (Num Char) Arising from use of worm
My code:
which :: [a] -> String
which x | x == [1,2,3,1,2,3,1,2,3] = "worm" | x == 3:2:1:snake = "snake" | otherwise = "F"
Is there another approach i'm missing? and why is my function giving me that error?
Two problems:
You can't have a function that returns a list of numbers sometimes and a string other times. That's literally the entire point of a strongly typed language. If you want something like that, you need to use a sum type.
You can't compare infinite lists. You can try, but your program will never finish.
I have the following groovy code:
def script
String credentials_id
String repository_path
String relative_directory
String repository_url
CredentialsWrapper(script, credentials_id, repository_name, repository_group, relative_directory=null) {
this(script, credentials_id, 'git#gitlab.foo.com:' + repository_group +'/' + repository_name + '.git', relative_directory);
}
CredentialsWrapper(script, credentials_id, repository_url, relative_directory=null) {
this.script = script;
this.credentials_id = credentials_id;
this.repository_url = repository_url;
if (null == relative_directory) {
int lastSeparatorIndex = repository_url.lastIndexOf("/");
int indexOfExt = repository_url.indexOf(".git");
this.relative_directory = repository_url.substring(lastSeparatorIndex+1, indexOfExt);
}
}
Jenkins gives me the following:
Unable to compile class com.foo.CredentialsWrapper due to hash collision in constructors # line 30, column 7.
I do not understand why, the constructors are different, they do not have the same number of arguments.
Also, "script" is an instance from "WorkflowScript", but I do not know what I should import to access this class, which would allow me to declare script explicitly instead of using "def"
Any idea ?
When you call the Constructor with four parameters, would you like to call the first or the second one?
If you write an constructor/method with default values, groovy will actually generate two or more versions.
So
Test(String x, String y ="test")
will result in
Test(String x, String y) {...}
and
Test(String x) {new Test(x, "test")}
So your code would like to compile to 4 constructors, but it contains the constructor with the signature
CredentialsWrapper(def, def, def, def)
two times.
If I understand your code correctly, you can omit one or both of the =null. The result will be the same, but you will get only two or three signatures. Then you can choose between both versions by calling calling them with the right parameter count.
I think I need some help with the OPL language :/
My code is the following:
using CP;
int NbMchs = ...;
range Mchs = 0..NbMchs-1;
tuple Mode {
int opId;
int mch;
int pt;
};
{Mode} Modes = ...;
// Not Working...
int test[m in Mchs] = all(md in Modes: md.mch == m) md.opId;
What I want to do is to extract m 1D arrays from the Modes structure containing just the opId field of the tuple. Each test[m] array has to contain it's corresponding elements: that is the opId field of the tuple md where md.mch =m.
The error that I get from the above code is "Cannot use type int[] for int". It seems like the right hand side of the above function is returning a single integer, but I was thinking that the all() operator is the one that I can use to do the job.
Thanks in advance
In the general case, the number of opId depends on the machine m so you cannot really have a 2-D array here. I would use an array of sets:
{int} test[m in Mchs] = { md.opId | md in Modes: md.mch == m };
Note that it assumes that you only have one mode per opId,mch.
I have just started learning groovy and I am reading "Groovy in Action".
In this book I came across a statement that it doesn’t matter whether you declare or cast a variable to be of type int or Integer.Groovy uses the reference type ( Integer ) either way.
So I tried to assign null value to a variable with type int
int a = null
But it is giving me below exception
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'null' to class 'int'. Try 'java.lang.Integer' instead
at Script1.run(Script1.groovy:2)
Then I tried to assign null value to a variable with type Integer
Integer a = null
and it is working just fine.
Can anyone help me understand how groovy behaves such way or the reason behind it?
The core problem is that primitives can’t be null. Groovy fakes that out with autoboxing.
If you store a null value in a number, you can’t store that in a int/long/etc field. It’s not correct to convert a null number to 0, since this might be valid values. Null means that no value or choice has been made yet.
int is a primitive type and it is not considered as an object. Only objects can have a null value while int value can't be null because It's a value type rather than a reference type
For primitive types, we have fixed memory size i.e for int we have 4 bytes and null is used only for objects because there memory size is not fixed.
So by default we can use :-
int a = 0
Groovy uses wrapper types in all time when you call primitives
int a = 100
assert Integer == a.class
groovy takes int and wrap it into Integer before using it value
But groovy cannot set int value to null, because variable is int(primitive type) but not Integer.
int a = 100
int b = 200
a + b not int + int, but Integer + Integer
As I understand it, Groovy uses the wrapper types if you use literals. For example:
def a = 11 // equivalent to Object a = 11. 11 is an java.lang.Integer
or
assert ['java.lang.Integer'] == [ 11, 22, 33 ]*.class*.name.unique()
Although if you use a specific type in your definition, the compiler has to perform casting.
You can do:
def a = 11
a = 'ssss'
but
int a = 11 // or int a
a = 'ssss'
gives GroovyCastException.
This what you see in your case
I need to write a method in Scala that overrides the toString method. I wrote it but I also have to check that if there is an element that is '1' I will change it to 'a', else write the list as it is with the string method. Any suggestions how this can be done?
What error are you getting? seems to work for me
val l = List(1, 2, 3)
println(this)
override def toString(): String = {
val t = l.map({
case 1 => "a"
case x => x
})
t.toString
}
getting List(a, 2, 3) printed out
I see from the comments on your question that list is a List[List[Int]].
Look at the beginning of your code:
list.map { case 1 => 'a'; case x => x}
map expects a function that takes an element of list as a parameter - a List[Int], in your case. But your code works directly on Int.
With this information, it appears that the error you get is entirely correct: you declared a method that expects an Int, but you pass a List[Int] to it, which is indeed a type mismatch.
Try this:
list.map {_.map { case 1 => 'a'; case x => x}}
This way, the function you defined to transform 1 to a and leave everything else alone is applied to list's sublists, and this type-checks: you're applying a function that expects an Int to an Int.