Pre Java 8, we have all seen code like this:
List<Pair<A, B>> someList = ....
for (Pair<A, B> item : someList) {
A leftItem = item.getLeft();
aMethodThatProcessesTypeA(leftItem);
}
Seems like it should be simple/trivial to do via a Stream:
someList.stream()
.map(i -> i.getLeft())
.???
However, now I get stuck - with Streams being lazy, I am not sure how to proceed. Can anyone enlighten me?
Try it this way:
someList.stream()
.map(Pair::getLeft)
.forEach(this::aMethodThatProcessesTypeA);
assuming the method aMethodThatProcessesTypeA() is defined by the same instance as the method running your Java 8 Stream Code shown above.
Related
I have a set like below.
php55MajorVersion=5.5
php55MinorVersion=5
php55Dir="${top_build_dir}/php/php-${php55MajorVersion}.${php55MinorVersion}-${ansdkBuild}"
php56MajorVersion=5.6
php56MinorVersion=6
php56Dir="${top_build_dir}/php/php-${php56MajorVersion}.${php56MinorVersion}-${ansdkBuild}"
php70MajorVersion=7.0
php70MinorVersion=4
php70Dir="${top_build_dir}/php/php-${php70MajorVersion}.${php70MinorVersion}-${ansdkBuild}"
Then I have the following input
phpVersion="php70"
phpMajorVersion="${phpVersion}MajorVersion"
phpMinorVersion="${phpVersion}MinorVersion"
phpDir="${phpVersion}Dir"
Now I want to print value associated with phpMajorVersion , which should be 7.0 (how -> phpMajorVersion -> ${phpVersion}MajorVersion -> php70->MajorVersion -> 7.0).
I have tried using $("${phpVersion}MajorVersion"), but am still getting , php70MajorVersion , but not 7.0 .
Any pointers on this?
You can get what you want using the getProperty method like this:
ext {
php55MajorVersion=5.5
php56MajorVersion=5.6
php70MajorVersion=7.0
phpVersion="php70"
}
println getProperty("${phpVersion}MajorVersion")
I believe it's not possible to achieve this using just braces, another StackOverflow answer suggests the same.
The following code gives the error
Expected 'while' in 'do-while' loop
if let path = NSBundle.mainBundle().pathForResource("Chapters", ofType: "txt"){
do {
let stringFromFile = try String(contentsOfFile:path, encoding: NSUTF8StringEncoding)
var chapters: [String] = stringFromFile.componentsSeparatedByString("#")
chapters.removeAtIndex(0)
} catch {
print((error))
}
}
it was working fine before, but now it's giving me an error. Does anyone know why?
That code works for me as-is in the Playground with the appropriate Chapters.txt file in the Resources folder; XCode 7.1 Build 7B60. Did you try Shift-Command-K for a Clean Build?
Something does not seem right with your error message. With Swift 2.0, there are no more do-while loops. They have been replaced by repeat-while loops instead. As your code snippet shows, do has been repurposed for do-try-catch error handling.
I have a function with the following signature:
static func dictionaryToJSON<K : ToJSON,V : ToJSON> ( dictionary : Dictionary<K,V>) -> JValue
You can find it here.
When I attempt to call this function, I get the error Cannot invoke 'dictionaryToJSON' with an argument list of type '(Dictionary<Domain, Account>)'. Here is the call:
let accounts : JValue = Aeson.dictionaryToJSON( self.accounts)
self.accounts has type Dictionary<Domain,Account>, and Domain and Account both implement the protocol ToJSON. Is there any reason why this doesn't type check? This is for Swift 2 (XCode 7 beta 6), so maybe there is a compiler bug?
It looks like I commented out Domain's implementation of ToJSON to debug and forgot. It's working now.
I have some Erlang code and Emacs is telling me that I have "Unbalanced parenthesis", but I can't figure what is wrong... Here is the code:
receive
{connect, Client} ->
NewPid = spawn_link(fun() -> game_loop(0,0)),
Client ! NewPid,
handle_connect()
end.
The error is at the row starting with NewPid...
You forgot the end after game_loop(0,0) in order to correctly completely define an anonymous function. The snippet should therefore look as follows:
receive
{connect, Client} ->
NewPid = spawn_link(fun() -> game_loop(0,0) end),
Client ! NewPid,
handle_connect()
end.
I'm totally confused about adding mongo data to template data. I haven't even started trying to get the data from a database as I can't get my templates to see test data (see below). This is in docpad.coffee for the moment, but ultimately g will be the output of mongoDB.
events:
extendTemplateData: (opts) ->
# {templateData} = opts
getGigsData: ->
g = { "date" : "3-4-2013", "location" : "Gent" }
return g
opts.templateData["getGigsData"] = getGigsData
And I hope to access it with <%= #getGigsData().date %>
Thanks so much for some guidance
I should add that this design is based on wanting to make it easy for the band to add gigs, without letting them edit the page content itself as I fear they would mess up the markup - if there are other ways to achieve this goal, I'd be pleased to hear.
Tried this locally. And hit the issue:
debug: Emitting the event: extendTemplateData
→ [2014-02-14 01:38:50.030] [/Users/balupton/Projects/docpad-extras/skeletons/so-21747504/node_modules/docpad/out/lib/docpad.js:1184] [DocPad.emitSerial]
error: Something went wrong with the action
→ [2014-02-14 01:38:50.037] [/Users/balupton/Projects/docpad-extras/skeletons/so-21747504/node_modules/docpad/out/lib/interfaces/console.js:107] [ConsoleInterface.destroyWithError]
error: An error occured:
ReferenceError: getGigsData is not defined
at Object.docpadConfig.events.extendTemplateData (/Users/balupton/Projects/docpad-extras/skeletons/so-21747504/docpad.coffee:42:44)
at ambi (/Users/balupton/Projects/docpad-extras/skeletons/so-21747504/node_modules/docpad/node_modules/ambi/out/lib/ambi.js:25:27)
at DocPad.<anonymous> (/Users/balupton/Projects/docpad-extras/skeletons/so-21747504/node_modules/docpad/out/lib/docpad.js:995:25)
at ambi (/Users/balupton/Projects/docpad-extras/skeletons/so-21747504/node_modules/docpad/node_modules/ambi/out/lib/ambi.js:23:18)
at Task.<anonymous> (/Users/balupton/Projects/docpad-extras/skeletons/so-21747504/node_modules/docpad/node_modules/event-emitter-grouped/out/lib/event-emitter-grouped.js:45:23)
at ambi (/Users/balupton/Projects/docpad-extras/skeletons/so-21747504/node_modules/docpad/node_modules/ambi/out/lib/ambi.js:23:18)
at fire (/Users/balupton/Projects/docpad-extras/skeletons/so-21747504/node_modules/docpad/node_modules/taskgroup/out/lib/taskgroup.js:163:25)
at b (domain.js:183:18)
at Domain.run (domain.js:123:23)
at Task.fire (/Users/balupton/Projects/docpad-extras/skeletons/so-21747504/node_modules/docpad/node_modules/taskgroup/out/lib/taskgroup.js:173:25)
at processImmediate [as _immediateCallback] (timers.js:330:15)
Which indicates that the error is actually inside our event handler, rather than inside our code. That for some reason getGigsData is not being set, despite our:
getGigsData: ->
g = { "date" : "3-4-2013", "location" : "Gent" }
return g
Examining the code, as a CoffeeScript user, I found the issue. As a non-coffeescript user, you can use the coffeescript compiler on the coffeescript website http://coffeescript.org to see the compiled javascript, which is:
({
events: {
extendTemplateData: function(opts) {
({
getGigsData: function() {
var g;
g = {
"date": "3-4-2013",
"location": "Gent"
};
return g;
}
});
return opts.templateData["getGigsData"] = getGigsData;
}
}
});
As we can see that is definitely not what we expected. We are just defining getGigsData inside an object, then doing nothing with it.
The issue is that we used a colon instead of an equals sign, so getGigsData: -> instead of getGigsData = ->. This is not a coffeescript thing, but you would have run into the same issue if this was javascript too, albeit javascript may be a bit more obvious due to the necessary squiggly braces around object definitions.
As a sidenote, if you prefer to use JavaScript with DocPad for whatever reason, that is totally supported. You could use a docpad.json or docpad.js file for your docpad configuration file. Another option, is to continue using CoffeeScript then just wrap JavaScript code within the backtick, see: http://coffeescript.org/#embedded