Pulling a specific character from a list SML - smlnj

Hello im still relatively new to SML/NJ and im struggling to figure out how I can pull a specific character from a tuple list, I was told that you explode the string then pull the specific value by calling it but calling the value is what im having issues with if anybody can help me with this I'd appreciate it.
Here the code :
val test = ("abcdefg", 3);
explode (#1 test):

Related

Scala - How to convert Map[Int,List[List[IntVar]]] to

I am new in Scala programming and the flatmap function gives me headache.
Before you ask me to search, I made some research on Stackoverflow : https://stackoverflow.com/search?q=scala+map+to+list but I didn't find something to solve easily my problem.
Here is my problem :
For a scala programming project with Jacop (http://jacopguide.osolpro.com/guideJaCoP.html) ,
I need to convert this kind of map
Map[Int,List[List[IntVar]]]
to :
List[T]
// In my case , only a List[IntVar]
I know I should use several times flatMap but I wonder how correctly use that in my case.
Thanks for your help
If you want every IntVar from the values of the map, you could to this:
map.values.flatten.flatten.toList
The call to values returns an Iterable containing all the values of the map. In this case, it returns an object of type Iterable[List[List[IntVar]]]. The first flatten-call on this object flattens it to an Iterable[List[IntVar]]. The second flatten-call flattens this object further to an Iterable[IntVar]. Finally, the toList method converts it to a List[IntVar].

How can I 'unparse' a parse tree in q/kdb+?

I am trying to create an automatic 'debugger' for tracing function flow. Because I'm not a god, I do make mistakes, and when I do, I normally end up throwing a bunch of "show" in my functions. What I'm looking to do is create a function that will insert shows prior to each line for each variable used in an expression on that line and any variable assigned to in the previous.
Imagine I have a function f that is throwing an unhelpful error. I would insert
f: debugwrap[f];
after the function definition to insert the appropriate debugging within the lines of function string, parse, and return the augmented function.
I have had success handling the params and simple functions, but where I have trouble is where semicolons do not indicate eol, such as in function calls. Using parse on the function body, I can easily break out all the lines and find the required variables, but once I do that, I need to 'unparse' each line in the function. That unparsing is giving me trouble, especially where functions are translated to what I believe is k - such as "*:".
Simple example with only initial logging:
q)f: {[a;b] a: a xexp b; c: a-first `int$-1#string first table[`symbols]; :c }
q)df: dp[f;";"]
q)df
"{[a;b] show "a is ",string[a]; show "b is ",string[b]; a : a xexp b;c : a - *:`int$-1#$:*:table`symbols;: c;}"
q)parse df
ERROR: *:
What I'm doing now is recursively walking through the parse tree and reconstructing the call. That is painful and not yet yielding results. What I think is the best way is to get the information I need out of each parse subtree, then unparse that subtree and append it to my function string.
Appreciate any help you all can offer.
The best place to see how debugging might be done, is with this code: http://code.kx.com/q/ref/debug/

How to write program in Spark to replace word

it is easy for Hadoop to use .replace() for example
String[] valArray = value.toString().replace("\N", "")
But it dosen't work in Spark,I write Scala in Spark-shell like below
val outFile=inFile.map(x=>x.replace("\N",""))
So,how to deal with it?
For some reason your x is an Array[String]. How did you get it like that? You can .toString.replace it if you like, but that will probably not get you what you want (and would give the wrong output in java anyway); you probably want to do another layer of map, inFile.map(x => x.map(_.replace("\N","")))

Passing long values into MLlib's Rating() method

I am trying to build a recommender system using Spark's MLlib library. (using Scala)
In order to be able to use the ALS train method , I need to build a rating matrix using the Rating() method (which is a part the package org.apache.spark.mllib.recommendation.Rating). The method requires an int be passed as the user id . However the dataset i am working with has 11 digit id's and hence throws an error when I try to pass it.
Does anyone know if there is some way around this where I can pass a long value into the Rating method ? Or someway to override this method ? Or someway to uniquely convert the 11 digit number to 10 or 9 digits while keeping it an int?
Any help will be greatly appreciated. Thanks
This will depend, I think, on the range of your ids. Can you simply take the Id modulo Int.MaxValue? That is:
(id % Int.MaxValue).toInt
or can you just hash it to an Int?
id.hashCode

Scala Option vs Java null [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why Option[T] ?
I am a newbie on Scala and I can't really feel that sensation of that difference between null on java and Option on Scala .
I know that none is an object and if I write smth like that on Scala , it will go safe :
val map = Map("koko" -> "is a cat")
val string:Option[String] =map.get("other")
println(string.map(a=>println(a.toString)) )
I well get None as a result instead of throwing an exception .
It's interesting .
However if I need the returning value not to be wrapped by Some . I will use .get to return the value .
In our example , it will throw an exceoption:
map.get("other").get.map(a=>println(a.toString))
I know I can handle this issue by using "match" .
I am taking here that I need to learn what's that booming on option on Scala vs null on java !
What I still couldn't get is that how could I use the advantages of Option but in case there are values existed into my variable , return the value not Some(value)
def testOption(): String = {
val map = Map("koko" -> "is a cat")
val string: Option[String] = map.get("koko")
string
}
I mean is there any way to make this code works instead of updating the return value to be Option[String] !
Imagine that I have to return a String in order to be set into my bean variable that is of type String not Option[String]
How could I make that works without any kind of match !!
I guess if there smth that makes it works , that will make me understand well the power of Option.
Options are actually quite useful. In Java, it's tough to document whether or not something can be null. In Scala, you can often assume all variables are not null.
This gets really useful when you have some computation that can fail or may not be defined. A good instance would be if you have a webapp and there's some user data that isn't filled out, so the computation can't run. If you had the user data in a map, you could do something like
val myData = map.get(userId).map(doFunction).map(toHtml)
println(myData.getOrElse(noDataHtml))
So clearly here we never have to worry about null. If the user data is in the map, we proceed with the computation and then turn that into html. Otherwise, we'll just print out a default html. In Java, at each stage we'd have to do a null check, but in Scala we can just chain functions.