Filtering: Exclude based on comparing two objects - comparison

I have a site where you can look for print shops on a map and filter what services they offer. Example is here: www.pigments.io
Each print shop has some properties stored in an object.
marker: {
processes: {
'digital': false,
'offset': false,
'silk': true,
'letterpress': false,
'engraving': false
},
finishings: {
'diecutting': true,
'lasercut': false,
'perforation': false,
'msg': false,
'softtouch': false,
},
products: {
'apparel': true,
'largeformat': false
}
}
The checkboxes return an object with the same structure, if the checkbox is checked the value is set to true. Now let's say if I want to only show silk screen shops if only silk screen has been checked under processes I could do something like this:
((marker.processes.silk == true && marker.processes.silk == formData.processes.silk) ||
The shop (marker) needs to be set to true with it's silk property & if the checkbox is checked it returns true. I'm using polymer, it filters over each marker object and evaluates the statement above. If it's true, it shows the marker. (I just realized I could have just done this:
((marker.processes.silk == true && formData.processes.silk == true))
Now of course I could do something like this.
((marker.processes.silk == true && formData.processes.silk == true) || (marker.processes.digital == true && formData.processes.digital== true))
and so on. That'll get ugly but it would theoretically work. But now if I introduce another field to be compared, say the finishings, I'm just getting completely lost here. Basically let's say I want to show only silk screen shops. Works as the code above. Now I only want to show the silk screen shops that do diecutting. I know how I could seperately look for only silk shops, or only diecutting shops. But now if I add those two, if diecutting is selected, this needs to exclude from the already filtered silk shops.
My brain is mushy, please help x_x. I've been trying to come up with the solution for this for over an hour, thoughts revolving in circles. I'm not even sure if this can be done with basic comparison operators anymore?
Also, I realize this approach is really ugly and inefficient.
/edit1:
I've just realized I should check underscore. Essentially if I'm not mistaken I just need to check if the print shop object's true values are the same as the checkbox object's values, right?
/edit2:
I think I need to get all the true properties from the marker object and compare them with the corresponding properties in the checkbox. If they are all true/identical, then the marker should be shown, right?
/edit3:
I nearly have it (I think if I was correct with assumption from edit2).
var markerProcessKeys = lodash.keys(lodash.pick(marker.processes, lodash.identity));
var markerProcess = lodash.pick(marker.processes, lodash.identity);
// doesn't work
var filterProcess = lodash.pick(formData.processes, markerProcessKeys);
//var filterProcess = lodash.pick(formData.processes, 'digital','offset');
// This one works, manually selecting only the true ones.
//So what's left to do is get the key names of the true ones and then compare
//I don't have it working yet though... I need to do the manual one automatically with lodash. Suggestions?
// this won't work
var = lodash.pick(markerProcess, filterProcess);
console.log(lodash.isMatch(t1, t2));
/edit4:
Nope, that's the wrong approach :/

Start by building a function that will tell you if a given shop matches a set of requirements. Assuming you requirements object looks like
{
processes: {
'digital': true
},
finishings: {
'diecutting': true
}
}
your function could be written as nested _.every calls :
function matches(requirements, shop) {
return _.every(requirements, function(properties, cat) {
return _.every(properties, function(val, key) {
// (!val) is there to ignore properties set to false
return (!val) || shop.marker[cat][key];
});
});
}
You would then use _.filter to extract the desired shops:
var validshops = _.filter(shops, _.partial(matches, requirements));
And a demo http://jsfiddle.net/nikoshr/9eq6o96L/

Related

iOS Realm detect changes for RLMObject

I have written code like this to listen for changes in Post object.
notification = Post.allObjects(in: RLMRealm.encryptedRealm()! as! RLMRealm).addNotificationBlock({ (results, changes, error) in
let pred = NSPredicate(format: "tag == %#", self.postTag)
self.posts = CommonResult.objects(with: pred).sortedResults(usingKeyPath: "id", ascending: true)
if let _ = changes {
if (changes!.insertions.count > 0 || changes!.deletions.count > 0 || changes!.modifications.count > 0) {
self.tblListing.reloadData()
}
}
})
In my Post object, there are 2 property. One is 'rowHeight' and another is 'isLikeByMyself'.
I want to reload tableview only if 'isLikeByMyself' is changed. How shall I do? Is it possible?
You have at least two options.
If you don't have many Post objects, you may want to consider registering object notifications on each of them. Object notifications tell you which properties were changed and how, so you can use that information to reload the table view. However, you will need to register a separate notification on each Post object, which may not be practical if you have a large number of them.
Here is an alternate idea. Add an ignored boolean property to Post called something like isLikeWasChanged, and add a Swift didSet block that will set isLikeWasChanged to true any time you modify isLikeByMyself. Then, in your existing collection observation block, reload the table view only if at least one isLikeWasChanged is true, remembering to set them all back to false before you leave the block.

Is boolean or nil comparison faster?

I am writing a code in Swift where I have a class as follows:
class Note
{
var isDeleted:Bool = false
var deletedDateTime:Date!
}
Suppose there is a data source that is filled with Notes and I want to make a filter based on comparison, then I can either compare by isDeleted or I can compare by deletedDateTime. Which one would be faster from the following two statements:
var dataSource:[Note] = [Note]()
// fill the datasource with objects
dataSource.filter({$0.isDeleted == false}) // 1
dataSource.filter({$0.deletedDateTime == nil}) //2
Can anyone please guide me?
There won't be much of a time difference, if you are talking about the execution time. But your class seem to be a bit obvious since you could remove the unnecessary boolean variable because as you have asked the both mean the same.

Check if SKCameraNode view contains a node of a certain class?

The SKCameraNode has two methods to it for checking node visibility inside it's viewport. (.containsNode() and .containedNodeSet())
The first return a bool, which is what I'm looking for. Checking for one node object works fine.
if myCamera.containsNode(mySpriteNode) == false {}
But I want to check wether it contains nodes of a class.
if myCamera.containsNode(MyClass()) == false {}
Since it doesn't work I wonder how this would be done.
Thank you.
You need to look at the other method you mentioned containedNodeSet(). This will return Set<SKNode> which you can then inspect as you wish, for example:
for node in cameraNode.containedNodeSet() {
if let interestingNode = node as? InterestingClass {
// Do something useful here
}
}

Swift: Random number arrays inside images and variables with a for loop

I am creating a game in which, depending on the number of 'swipes' chosen to do, (let's say 3), 3 different patterns show on the screen, one by one. I am working on developing the first pattern.
So I have this:
if (swipes.no_of_swipes) == 3 {
swipeArray = Array<UInt32>(count: 3, repeatedValue: 0)
for i in 0 ..< 3 {
swipeArray[i] = arc4random_uniform(84)}
}
As far as I am aware, this code creates an array with three UInts which can be accessed by doing swipeArray[0], swipeArray[1], and swipeArray[2]. My first question is how long will this swipeArray stay the same? Until the close the view? Should I have a 'refresh button' when the user loses - and if so, how would I make one?
Then I have a property observer. You will notice the for loop, which I am using to keep code concise. I understand that I could do something like x++ somewhere in here so that it will go through each one.
var playBegin: Bool = false{
didSet {
if playBegin == true {
println("\(playBegin)")
var swipes = Menu()
if (swipes.no_of_swipes) == 3 {
for i in 0 ..< 3 {
patternRoom.image = UIImage(named: "pattern\(swipeArray[x])")
//rest of code
}
}
}
The pattern image comes from a set of 84 images named like pattern7 and pattern56. My second question is, how could I code the for loop to go through each swipeArray[x].
Thank you in advance,
Will
how long will this swipeArray stay the same?
This is a bit too open ended. It’ll stay the same until you assign a new value to it, either from this same bit of code or a different part. Only you can know when that will be, by looking at your code.
Since you express an interest in keeping the code concise, here’s a couple of code tips.
You might think about writing your first snippet’s loop like this:
swipeArray = (0..<swipes.no_of_swipes).map { _ in
arc4random_uniform(84)
}
This combines creating a new array and populating the values. By the way, just in case you don’t realize, there’s no guarantee this array won’t contain the same value twice.
It’s also probably better to make swipeArray of type [Int] rather than [UInt32], and to convert the result of arc4random to an Int straight away:
Int(arc4random_uniform(84))
Otherwise the UInt32s will probably be a pain to work with.
For your second for loop, you can do this:
for i in swipeArray {
patternRoom.image = UIImage(named: "pattern\(i)")
// rest of code
}
When writing Swift, usually (but not always), when you find yourself using array[x] there’s a better more expressive way of doing it.

Type True/False in Umbraco

We want to implement a check box[Type : true/false] in an DocumemtType in Umbraco.
Our current Project necessity is:
an check box which will decide whether an image should be an link or popup
The code goes this way ...
var child= #Model;
if(child.GetProperty("popUp").Value.ToString() == "1")
{
// true means image will act as popup
}
else
{
// false means image will act as link
}
But the problem is an error is occurred "Cannot perform runtime binding on a null reference"
I have also tried code like ,
if (child.GetProperty("popup").Value.Equals("1"))
{
}
or
if (child.GetProperty("popup").Value.ToString().Equals("1"))
{
}
but still not able to get it. All suggestions are welcomed .
node.GetProperty("popUp") is the way to go. If your control value is actually string, then your check logic would look like
if (node.GetProperty<string>("popUp") == "1"){}
Effectively generic GetProperty is what your code does, but it handles the null case, returning default(string).
(I have never used the dynamic thing, in case something will go wrong there, do the typed var node = new Node(id);)
Since you recently added the property to the document type, unless each node of that type has been published, the property will return null. You'll need to check if the property is null first then check if its true.
var popUp = child.GetProperty("popUp");
if (popUp != null && popUp.Value.Equals("1"))
{
// popup...
}
else
{
// link...
}
Used the below code and it worked fine for me
var child= #Model;
if(#child.popUp)
{
// true means image will act as popup
}
else
{
// false means image will act as link
}
Use this:
var child= #Model;
if(child.GetPropertyValue<bool>("popUp", false))
{
// true means image will act as popup
}
else
{
// false means image will act as link
}

Resources