The abs method of the purescript-numbers package is documented here:
https://pursuit.purescript.org/packages/purescript-numbers/9.0.0/docs/Data.Number#v:abs
However, consider this simple code example which implements this method:
import Data.Number (abs, toNumber, isFinite, sqrt)
j :: Number
j = abs -32.5
This produces the following error:
Cannot import value abs from module Data.Number
It either does not exist or the module does not export it.
Is this a bug, or intended behavior?
What is the correct way to import / use the abs method of the Data.Number library?
My suspicion is that you're probably using PureScript compiler v0.14.x and a corresponding package set 0.14.x, but at the same time are looking at the latest versions of the libraries on Pursuit.
Just about a week ago, the new version of PureScript compiler 0.15.0 came out, and with it came many breaking changes (most notably, ES-format foreign modules), and as is tradition in such cases, the community took the opportunity to do some refactoring while we're at it.
One instance of such refactoring was moving the abs function (as well as many other functions) from the Math module (in the math library) to the Data.Number module (in the numbers library).
This means that if you're using PureScript 0.15 and higher, abs is in Data.Number, but if your version of PureScript is lower, the function is in the Math module.
Related
I am trying to find an equivalent of Verilog power operator ** in Chisel. I went through Chisel Cheat sheet and tutorial but I did not find what I was looking for. After going through designs written in Chisel, I found that log2xx functions are popular choice while the power operator is never used. Of course I can always use the shift operator to get power of 2 but I was hoping that there is general power operator in Chisel. I tried to use scala's math functions to the job but I got compilation error.
Since you are trying to calculate a bitwidth which is calculated at elaboration time (ie. when Scala is elaborating the hardware graph), we can use Scala functions. Scala only provides a power function for Doubles, but that works just fine for this case. Try math.pow(base, exp).toInt, note that base and exp can both be Ints and Scala will automatically convert them to Doubles for the function call. You simply need to convert the resulting Double to an Int for use as a bitwidth.
I am looking for an equivalent to Mathematicas RootApproximant function in python or a python lib:
https://reference.wolfram.com/language/ref/RootApproximant.html
Basically, this function finds the algebraic root of a numeric, e.g.
RootApproximant[1.414213] -> sqrt(2)
Thanks!
Look at sympy.nsimplify (which uses mpmath.identify internally)
>>> nsimplify(1.414213, tolerance=1e-6)
√2
You might be looking for mpmath.identify():
In [295]: import mpmath as mpm
In [296]: mpm.identify(1.414213)
Out[296]: 'sqrt(((4-sqrt(0))/2))'
In [297]: mpm.identify(3.141592/2)
Out[297]: '(2**(157/183)*3**(67/183)*5**(152/183))/(7**(59/61))'
In [298]: mpm.identify(3.141592/2,['pi'])
Out[298]: 'pi*((4-sqrt(0))/8)'
It's obviously not as clear-cut as Mathematica's implementation, but I'm sure it beats any manual attempt to do this job. And the features are surely different (and more scarce), so you should check out the manual if it fits your needs.
I'm using a utility library for dimensional analysis that i'd like to extend with my own units, and I'd like to be able to write
import my.util.units._
in files in my project. My thought was to define
package my.util
object units {
import squants._
[... other definitions ...]
}
and I expected import my.util.units._ to have the same effect as import squants._, plus the other definitions. But it seems importing units._ doesn't end up adding squants._ to the scope.
Is there a way to do this in scala?
We've dealt with this a little bit at work, and we've tried to resolve this a few ways. Here's an example of how we import rabbitmq types throughout scala-amqp:
package com.bostontechnologies
package object amqp {
type RabbitShutdownListener = com.rabbitmq.client.ShutdownListener
type RabbitConnection = com.rabbitmq.client.Connection
type RabbitChannel = com.rabbitmq.client.Channel
type RabbitConsumer = com.rabbitmq.client.Consumer
type RabbitAddress = com.rabbitmq.client.Address
...
}
So now when we import com.bostontechnologies.amqp._ we get access to the rabbitmq types that we've defined. I know it requires quite a bit of duplication, however we've found it to be somewhat useful, especially since it gives us granularity over type names.
Also, you don't need to use a package object, we mainly use it for convenience of automatically importing our types around a package. You could just use a normal object as well.
Imports are not transitive in Java or Scala. Probably the closest you are going to get to what you seek is to create an object (perhaps a package object) with a type definition for each type of interest.
Back when reflection was still incipient, on the days of Scala 2.10.0 milestones, I asked a question about how could I use it to see the trees of code snippets from REPL. The excellent answer went further than I asked, and showed how they can be used to parse and evaluate trees as well, so I went ahead and tried to use that on a little project I had going on today.
Unfortunately, code parsed and evaluated that way doesn't seem to see any REPL definition:
scala> val x = 1
x: Int = 1
scala> import scala.tools.reflect.ToolBox
import scala.tools.reflect.ToolBox
scala> val tb = scala.reflect.runtime.universe.runtimeMirror(
getClass.getClassLoader).mkToolBox()
tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = ...
scala> tb.eval(tb.parse("x"))
scala.tools.reflect.ToolBoxError: reflective compilation has failed:
not found: value x
Is there a way to get it to recognize definitions made on REPL?
Recently I dug into repl, when trying to make it support type macros, so I'm well equipped to explain why it doesn't work. Getting it to work would be the next step :)
I know that you know that every snippet entered into repl gets wrapped into some boilerplate before being compiled. Therefore that x ends up being a field in a nested-nested-nested object in a package with a weird name.
Apparently, repl keeps track of all defined symbols and then injects the necessary imports along with the boilerplate it generates. Therefore subsequent lines can see that x unqualified. To the contrast, toolboxes simply reuse repl's classloader, but don't do anything about the imports, hence the failure.
A workaround would be to somehow get to an object representing a repl, ask it about defined symbols and then generate corresponding imports into the code that you feed to a toolbox. If you file a ticket, I'll try to code up a workaround after the 2.10.1 code freeze madness ends (supposedly, end of this week).
Someday, I'd like to learn Scala. What I see about the language from people who like it is very encouraging.
Today, though, is not that day. Today, I'd just like to make some changes to my team's build file. Unfortunately, this build file was put together with SBT, and is nearly incomprehensible.
My main issue is that it appears to me that SBT introduces some huge collection of new operators that do things with strings and lists to create some sort of sbt object. For example, in sbt:
"args4j" % "args4j" % "2.0.12"
Apparently is actually defined; however, I can't even tell what type it is at the scala repl, since at the repl I get the sensible error:
scala> val tstcrap = "args4j" % "args4j" % "2.0.12"
<console>:6: error: value % is not a member of java.lang.String
val tstcrap = "args4j" % "args4j" % "2.0.12"
I get this error even after setting up the classpath to include the sbt-launch.jar file and doing import sbt._.
Likewise, I'm dealing with stuff like this:
val jarSources = (descendents(classesOutput ##, "*") ---
assemblyExclude(classesOutput ##))
What's that ## operator, what's that --- doing, and more importantly what is the type of this expression? Are all these new operators documented somewhere, and is there some way to get a scala repl that's using the same language as is used in the sbt build files?
Looking at this sbt file reminds me of trying to decipher perl without ever reading any of the relevant man pages. (Not a recommended activity)
Update: After looking at the links in the answer below, and looking at other questions and answers tagged sbt, I've come across the major piece of scala knowledge that I was missing: scala allows one to define implicit conversions that will be invoked before methods are resolved. In this case, sbt defines inside the ManagedProject trait, an implicit conversion from String to the private class sbt.GroupID, so that
"a" % "b"
Is really something like
(new GroupID("a")) % "b"
I imagine the resolution order and other rules around implicit conversions must get very complicated; it almost reminds me of the nightmares you can introduce in C++ with operator overloading when done through non-member functions.
Since an SBT build file is a full-fledged Scala source file and relies on some libraries provided by SBT itself, it's difficult to cover SBT well without relying on some familiarity with Scala. I'm not aware of such a guide.
For the specific questions you raise, I think these wiki pages will help:
% operator for strings: http://code.google.com/p/simple-build-tool/wiki/LibraryManagement
## and --- operators: http://code.google.com/p/simple-build-tool/wiki/Paths
If you want to get a Scala REPL running with the SBT libraries available, try this:
$ sbt console-project
Some other useful commands are listed at http://code.google.com/p/simple-build-tool/wiki/RunningSbt .
Update 2016 (5 years later).
This is not a complete guide, but the article "Sbt heiroglyphs and multi-projects explained" from Divan Visagie can help starting to use sbt.
Plus, the sbt documentation is quite complete nowadays, and covers multiple projects in a single build.
The '---' operator is described in the PathFinder (since the 0.2 version).