I ran into an issue where an intersection type wouldn't work with $Diff. I ran into this when trying to implement a react HOC for a component that had an intersection prop.
A simplified example looks like:
type One = { one: number }
type Two = { two: number }
type Three = { three: number }
type Both = One & Two
type All = Both & Three
const both:$Diff<All, Three> = { one: 1, two: 2 }
^ Cannot instantiate `$Diff` because undefined property `three` [1] is incompatible with number [2].
References:
6: type Both = One & Two
^ [1]
5: type Three = { three: number }
^ [2]
If there is a way to correct this, I use One and Both as prop typings for components, and All and $Diff is part of the HOC typing.
Thanks!
https://flow.org/try/#0PTAEAEDMBsHsHcBQAXAngBwKagPIDtsBeUAb1FgIC5Q8BXAWwCNMAnUAXxQ2wBV5ZQxMsn7U6TVhy5ZQPABYtMRUqGQKlYhszac0MgEKw1g3AVAAyWf2nYAgtGgnDxy-MWZEiAMYUAzslBGIzlKABIAEQBLSEgAHntoABpZdUwAPhMyCkxqAEZkkVhqACYOIA
I feel there are some weaknesses in the way flow handles intersection types, compounding them should work but doesn't as expected.
If you can define All as the intersection of One, Two, and Three, it will work as expected.
$Shape<> can sometimes get you around this sort of problem but I couldn't get it to play on this occasion.
// #flow
type One = { one: number }
type Two = { two: number }
type Three = { three: number }
type Both = One & Two
type All = One & Two & Three
const both:$Diff<All, Three> = { one: 1, two: 2 }
Related
I need to find how many digits there are in Price area after delimeter to compare
I used this code but not working properly because all region dont use same delimeter
Do you have better idea ?
strSalesPrice = conPeek(record,5);
delimeterPosition = strFind(strSalesPrice,",",1,strLen(strSalesPrice));
if (delimeterPosition!=0)
{
afterDelimeter = subStr(strSalesPrice,delimeterPosition+1,strLen(strSalesPrice));
if(strLen(afterDelimeter) > SalesParameters.DfcDigitSize)
{
throw error("Please check the number of digits after the decimal point");
}
}
Title is a bit confusing and uses mathy words, so just take a look at this C function:
void myfun(uint stepcount,float step,float base,int *step_array) {
for (uint i=0;i<stepcount;i++) {
base += step;
step_array[i] = (int)floor(base);
}
}
Given stepcount and step (always between 0 and 1), how can I tell if two base values will write the same results into step_array?
I am trying to style just the decimals to look just like this:
Didn't had success, I guess that I need to make my own filter, tried but didn't had success either, I guess it is because I am using it inside a state.
Here the code I am using for the number:
<h2><sup>$</sup>{{salary | number:0}}<sub>.00</sub></h2>
Inside the .app iam using this scope:
$scope.salary = 9000;
Thing is, number can be whatever the user salary is, it get the number from an input, in other places I have more numbers with decimals too.
Possible solutions:
Extract only the decimals from value and print them inside de
tag.
Use a filter to do this?
Use a directive that will split the amount and generate the proper HTML. For example:
app.directive('salary', function(){
return {
restrict: 'E'
, scope: {
salary: '#'
}
, controller: controller
, controllerAs: 'dvm'
, bindToController: true
, template: '<h2><sup>$</sup>{{ dvm.dollar }}<sub>.{{ dvm.cents }}</sub></h2>'
};
function controller(){
var parts = parseFloat(this.salary).toFixed(2).split(/\./);
this.dollar = parts[0];
this.cents = parts[1];
}
});
The easiest solution would be to split out the number into it's decimal portion and the whole number portion:
var number = 90000.99111;
console.log(number % 1);
Use this in your controller, and split your scope variable into an object:
$scope.salary = {
whole: salary,
decimal: salary % 1
}
Protip: Using an object like this is better than using two scope variables for performance
I have some data stored in this format (except many cases more):
var data = [
{"name":"John", "team":"team1"},
{"name":"Megan", "team":"team2"},
{"name":"Rupert", "team":"team2"},
{"name":"Albert", "team":"team1"}
];
I want to create this:
var colourScale = d3.scale.ordinal()
.range(a)
.domain(b)
"a" being an array of all levels of "team" (i.e. ["team1","team2"] in this case).
"b" being an array of ordinal colours of the same length as "a".
colourScale() should take the "team"-value as input and return a unique colour for each team.
How do I create "a" and "b"? Is there something equivalent to R's levels(data[ ,"team"]) in javascript or d3.js?
It's not exactly your solution as you don't explicitly find your specified a or b, but I found
var colourScale = d3.scale.category10();
function colour(d) { return d.team; }
and on the object you want to colour, bound to the appropriate data, chain
.style("fill", function(d) { return colourScale(colour(d)); });
has the effect that I assume you're searching for. Hope this helps.
I think there are some extra steps you need to do. You need first to create a 'unique' set of possible domain values. Then you can create a function that maps each value to a color. Something like this perhaps:
// A function to get unique values, can be optimized further
function unique(x) {
return x.reverse().filter(function (e, i, x) {return x.indexOf(e, i+1) === -1;}).reverse();
}
var data = [
{"name":"John", "team":"team1"},
{"name":"Megan", "team":"team2"},
{"name":"Rupert", "team":"team2"},
{"name":"Albert", "team":"team1"}
];
// An arbitrary set of colors
colors = ['#005824','#1A693B','#347B53','#4F8D6B','#699F83','#83B09B','#9EC2B3','#B8D4CB','#D2E6E3','#EDF8FB','#FFFFFF','#F1EEF6','#E6D3E1','#DBB9CD','#D19EB9','#C684A4','#BB6990','#B14F7C','#A63467','#9B1A53','#91003F']
//Create the ordinary scale based on your color set above
colorScale= d3.scale.ordinal()
.domain(unique(data.map(function(d){return d.team;})))
.range(colors)
Then you can access the color by:
.style("fill",function(d){return colorScale(d.team);})
Hope this helps.
I'm trying to write a trait (in Scala 2.8) that can be mixed in to a case class, allowing its fields to be inspected at runtime, for a particular debugging purpose. I want to get them back in the order that they were declared in the source file, and I'd like to omit any other fields inside the case class. For example:
trait CaseClassReflector extends Product {
def getFields: List[(String, Any)] = {
var fieldValueToName: Map[Any, String] = Map()
for (field <- getClass.getDeclaredFields) {
field.setAccessible(true)
fieldValueToName += (field.get(this) -> field.getName)
}
productIterator.toList map { value => fieldValueToName(value) -> value }
}
}
case class Colour(red: Int, green: Int, blue: Int) extends CaseClassReflector {
val other: Int = 42
}
scala> val c = Colour(234, 123, 23)
c: Colour = Colour(234,123,23)
scala> val fields = c.getFields
fields: List[(String, Any)] = List((red,234), (green,123), (blue,23))
The above implementation is clearly flawed because it guesses the relationship between a field's position in the Product and its name by equality of the value on those field, so that the following, say, will not work:
Colour(0, 0, 0).getFields
Is there any way this can be implemented?
Look in trunk and you'll find this. Listen to the comment, this is not supported: but since I also needed those names...
/** private[scala] so nobody gets the idea this is a supported interface.
*/
private[scala] def caseParamNames(path: String): Option[List[String]] = {
val (outer, inner) = (path indexOf '$') match {
case -1 => (path, "")
case x => (path take x, path drop (x + 1))
}
for {
clazz <- getSystemLoader.tryToLoadClass[AnyRef](outer)
ssig <- ScalaSigParser.parse(clazz)
}
yield {
val f: PartialFunction[Symbol, List[String]] =
if (inner.isEmpty) {
case x: MethodSymbol if x.isCaseAccessor && (x.name endsWith " ") => List(x.name dropRight 1)
}
else {
case x: ClassSymbol if x.name == inner =>
val xs = x.children filter (child => child.isCaseAccessor && (child.name endsWith " "))
xs.toList map (_.name dropRight 1)
}
(ssig.symbols partialMap f).flatten toList
}
}
Here's a short and working version, based on the example above
trait CaseClassReflector extends Product {
def getFields = getClass.getDeclaredFields.map(field => {
field setAccessible true
field.getName -> field.get(this)
})
}
In every example I've seen the fields are in reverse order: the last item in the getFields array is the first one listed in the case class. If you use case classes "nicely", then you should just be able to map productElement(n) onto getDeclaredFields()( getDeclaredFields.length-n-1).
But this is rather dangerous, as I don't know of anything in the spec that insists that it must be that way, and if you override a val in the case class, it won't even appear in getDeclaredFields (it'll appear in the fields of that superclass).
You might change your code to assume things are this way, but check that the getter method with that name and the productIterator return the same value and throw an exception if they don't (which means that you don't actually know what corresponds to what).
You can also use the ProductCompletion from the interpreter package to get to attribute names and values of case classes:
import tools.nsc.interpreter.ProductCompletion
// get attribute names
new ProductCompletion(Colour(1, 2, 3)).caseNames
// returns: List(red, green, blue)
// get attribute values
new ProductCompletion(Colour(1, 2, 3)).caseFields
Edit: hints by roland and virtualeyes
It is necessary to include the scalap library which is part of the scala-lang collection.
Thanks for your hints, roland and virtualeyes.