Using Clojure, how do I create the following object? The object is taken from java code (From Effective Java):
NutritionFacts cocaCola =
new NutritionFacts.Builder(240,8).calories(100).sodium(35).carbohydrate(27).build();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
While it’s hard to argue with the concision in the other answers1,
..has somewhat fallen out of favor, replaced by the more versatile->. Personally I prefer :It’s a couple more characters, but you gain two things:
.is right there.Most importantly, every other answer to this question has gotten the classname wrong: Java’s NutritionFacts.Builder is language sugar over the real JVM class named NutritionFacts$Builder, and that class is the one Clojure must refer to (since we are not using javac to compile our code).
1 I do disagree with the
dotosuggestion: it works only because this Builder class happens to implement its method-chaining by mutating a single instance and then returning it.dotois great for Java objects that require in-place mutation, but when a class is kind enough to pretend it’s immutable you should really be using the method-chaining version (ie,->).