Map laziness in clojure - swing

I am building a simple swing GUI in Clojure. I am trying to apply a single function to multiple GUI components by using map in the context of a let:
(map #(f % component4) [component1 component2 component3])
Where the components are all defined in the let.
Problematically, map is lazy, and the action is not applied to the components, however, I can force it by wrapping the above in a 'take'.
Is there a non lazy alternative to map? Or should I be going about this differently?
EDIT:
Using counterclockwise in eclipse. I had different results using (use 'Lib :reload) from the REPL and using CTRL+Enter from the editor. Reloading would launch the GUI, but the problem described above would occur. The problem did not occur when using CTRL+Enter from the editor, therefore I think my description of the problem may be inaccurate. Regardless, doseq seems to be a better alternative to map in this scenario.

I challenge your assertion that getting take involved makes any difference at all. If you wrapped it in doall or dorun it would do what you want, but you should consider using doseq instead of map for this sort of side-effect-only action.
Note
Originally posted as comment on question; copied to answer by popular demand.

doseq is probably the best way to approach this. doseq is roughly equivalent to a "for-each" statement that loops over each element of a collection in many other languages. It is guaranteed to be non-lazy.
(doseq
[comp [component1 component2 component3]]
(f comp component4))
Some general advice:
Use map and its lazy friends (including take, drop etc.) when you want a sequence as an output
Use doseq, doall, dotimes etc. when you are more interested in the side effects

Wrapping your map in a doall will force its evaluation. or a better alternative is doseq which is used for things involving side effects.

Related

Bind html to multiple .ts file in angular

I'm currently working on an Angular app, specifically a quite complex table, in terms of styling and features.
My component file currently has 2k lines of code, and it comprehends functions for styling text, styling the table, functions for check if data treated are correct, data formatting and so on...
Many of theese funtions are called directly from the HTML fiel thorugh interpolation.
Is there any way to break up this quite large file into smaller ones?
You can break up your component into smaller ones and nest them.
Typical example:
<app-list>
<app-list-item></app-list-item>
</app-list>
The parent component can then pass its properties down to the child components:
<app-list>
<app-list-item [name]="valueFromParent"></app-list-item>
</app-list>
It is further possible to emit values back up from the child to the parent:
<app-list>
<app-list-item (onChildEvent)="updateParent($event)"></app-list-item>
</app-list>
I tried to keep it simple here, but there is more to it.
I suggest going through the official Angular tutorials, because they explain these concepts pretty well.
You should further try to put as much functionality into Services as possible. This also helps to make your components smaller, easier to reason about and helps testing.
E.g. a functions for check if data treated are correct would be a good example for a service method.

How to add hyperlink on specific word

I am planning to develop a web application which will be used to published articles.Now i want to know how content is written something like wiki where some specific words contain hyperlinks.
In this content you can see Hashtable, Java programmer, JSTL foreac tag for this are marked has a hyperlink. Is there any tools exist or i have to associate hyperlink manually.
HashMap in JSP, or any other Map implementation e.g. <a>Hashtable</a>, I personally prefer (JSTL foreach tag for this). As a <a>Java programmer</a>, I often have urge to use Java code directly in JSP using scriptlet, but that's a bad coding practice, and one should always avoid that. Infact by smart use of expression language and JSTL core tag library, you can reduce lot of Java code from JSP. In our last post, we have seen example of JSTL foreach tag to loop over List, but not a Map, and that creates a doubt in one of m
The short answer is that you need to implement this in Javascript. The problem is horribly complicated because you need to iterate over a list of search_words while simultaneously walking through the body searching for longest matches. The longest match part is what makes this tricky: You want to match the text "Java Programmer", not just the word "Java".
You also want to ignore whitespace and capitalisation problems. I strongly recommend you build on an existing solution such as the open-source MediaWiki project to save reinventing a complicated wheel.

When to subclass and when to make do with existing class?

I want to dispatch events to announce the progress of an asynchronous process. The event should contain 2 properties: work done and work total.
As the name suggests ;) i could use ProgressEvent; it has bytesLoaded and bytesTotal properties that i can use. However, my async process isn't loading bytes, its processing pixels, so the property names are a bit misleading for my use case - although the class name is perfect.
The alternative is to create a custom event with two properties that i can name how i like. But this means another class added to the code base.
So my question is; Is it better to reuse an existing class where the properties are suitable but maybe the naming isn't ideal; Or to create a custom class that perfectly fits the requirement? Obviously, one extra class is no big deal, but OOP is all about reusing stuff so adding an unnecessary class does make me uneasy.
I await your thoughts...
PS: This is my first question on stack so be gentle
For clarity, I'd create a new class. Adding a new class is not much overhead at all, especially for something simple like an event. I find that code is more readable when I don't have to make mental translations (like bytesLoaded really means pixelsLoaded). To me this is akin to choosing poor names for variables.
In addition, by going the other route and re-using the ProgressEvent class, I would feel compelled to document the code to indicate that we're dealing with pixels rather than bytes. That starts to get messy if you have a lot of classes that uses the event.
Re-use is great, but I'd opt for clarity as long as it doesn't impact your productivity or the app's performance.
writing in doc clearly, using custom event or ProgressEvent is well.

Does this pattern have a name?

Disclaimer: I'm trying to learn proper OO programming/design, so I'm pretty new to this stuff.
I guess this is a general design patterns question, but I'll base my example on a game engine or something that renders objects to the display.
Consider the following:
hierarchy http://img31.imageshack.us/img31/9633/diagrame.png
How can this sort of separation between physical objects (e.g., cubes, spheres, etc.) and the rendering mechanism be achieved in an extensible manner?
This design is not set in stone, and perhaps I've got something wrong from the start. I'm just curious as to how a problem like this is solved in real world code.
That would be the Adapter pattern, or it could be implemented as a Strategy pattern.
The renderer should not be extended by the objects which he is supposed to draw. (Just my opinion) an object in your world is NOT a renderer but the renderer uses objects.
So you have maybe:
Interface IRenderer which defines a function draw(BasicObject).
Then your objects just extend BasicObject to be handled by the/a renderer.
As I said just my opinion. :)
Strategy patern it is.
I would use a Visitor pattern here.
Where the Visitor is the renderer and were the Visited is the 3D/Object.
I would also make the 3D/Object a composite.

Rails - Escaping HTML using the h() AND excluding specific tags

I was wondering, and was as of yet, unable to find any answers online, how to accomplish the following.
Let's say I have a string that contains the following:
my_string = "Hello, I am a string."
(in the preview window I see that this is actually formatting in BOLD and ITALIC instead of showing the "strong" and "i" tags)
Now, I would like to make this secure, using the html_escape() (or h()) method/function.
So I'd like to prevent users from inserting any javascript and/or stylesheets, however, I do still want to have the word "Hello" shown in bold, and the word "string" shown in italic.
As far as I can see, the h() method does not take any additional arguments, other than the piece of text itself.
Is there a way to escape only certain html tags, instead of all? Like either White or Black listing tags?
Example of what this might look like, of what I'm trying to say would be:
h(my_string, :except => [:strong, :i]) # => so basically, escape everything, but leave "strong" and "i" tags alone, do not escape these.
Is there any method or way I could accomplish this?
Thanks in advance!
Excluding specific tags is actually pretty hard problem. Especially the script tag can be inserted in very many different ways - detecting them all is very tricky.
If at all possible, don't implement this yourself.
Use the white list plugin or a modified version of it . It's superp!
You can have a look Sanitize as well(Seems better, never tried it though).
Have you considered using RedCloth or BlueCloth instead of actually allowing HTML? These methods provide quite a bit of formatting options and manage parsing for you.
Edit 1: I found this message when browsing around for how to remove HTML using RedCloth, might be of some use. Also, this page shows you how version 2.0.5 allows you to remove HTML. Can't seem to find any newer information, but a forum post found a vulnerability. Hopefully it has been fixed since that was from 2006, but I can't seem to find a RedCloth manual or documentation...
I would second Sanitize for removing HTML tags. It works really well. It removes everything by default and you can specify a whitelist for tags you want to allow.
Preventing XSS attacks is serious business, follow hrnt's and consider that there is probably an order of magnitude more exploits than that possible due to obscure browser quirks. Although html_escape will lock things down pretty tightly, I think it's a mistake to use anything homegrown for this type of thing. You simply need more eyeballs and peer review for any kind of robustness guarantee.
I'm the in the process of evaluating sanitize vs XssTerminate at the moment. I prefer the xss_terminate approach for it's robustness—scrubbing at the model level will be quite reliable in a regular Rails app where all user input goes through ActiveRecord, but Nokogiri and specifically Loofah seem to be a little more peformant, more actively maintained, and definitely more flexible and Ruby-ish.
Update I've just implemented a fork of ActsAsTextiled called ActsAsSanitiled that uses Santize (which has recently been updated to use nokogiri by the way) to guarantee safety and well-formedness of the RedCloth output, all without needing any helpers in your templates.