• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

From Java to Clojure - Trouble with function

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im coming from a Java background and i am trying to solve a problem that i have solved in Java. The Java problem i have written is here at http://pastebin.com/aRRdMCn4 I want to convert it to Clojure
So far i have only got this but keep running into many many troubles with it. Can anyone give me some pointers on where i am going wrong??

Im at the stage currently where i want to pass "word" in to transfrom2 and then let "x" be the sorted version of "word"

Once i have done this i want to put the original "word" and sorted version in to "x" into a sort of map/hashmap structure, just like i have in the Java solution




Hopefully you guys can help me convering the solution. Like i say this is my first functional language so go easy on me
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand what you want your functions to produce. Maybe you could provide an example of the input and expected output?

Your code looks very imperative, like you're trying to write Java-in-Clojure, so maybe step back and work through some basic functional programming tutorials in Clojure, to help you think about the task differently. For example, have a look at how map functions work, instead of trying to use a for-loop.

This free online course is a good place to start getting into the FP mindset with Clojure.
 
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your Java code, getword is the only pure function: it takes a string (w) and returns a string, with no side-effects on the external world. You're already fairly close to the Clojure equivalent:

You don't say what "many troubles" you are actually having so it's rather hard to help you.

I will note that your transform2 function prints x and returns the result of that (prn x) call which will be nil so that's probably tripping you up.

Also note that for, like map, is "lazy" and doesn't produce results unless you actually consume them with another function.

Going back to your Java code, process() uses side-effects to achieve its goals, via the map and ws variables. Since your program processes a list (of strings) as input and produces a subset of those strings as output, the equivalent in Clojure could use reduce in some way.

I think your Java code is intended to produce the list of words that are unique when viewed as sorted, lower-case strings? Is that correct?

Something else to consider in Clojure is the frequencies function which takes a collection and produces a hashmap from items to the number of occurrences (so (frequencies (map get-word words)) would give you the occurrences for each lower-case, sorted word in your original list of words). Then you could transform that into a set of the items that only occur once, and use the set to filter the original words (something like (filter #(unique-words (get-word %)) words) since a set can be used as a predicate).

I'll let you think about all that for a bit and if you're still stuck in a day or two, I'll show one possible solution (but there are many ways to solve this).
 
freeman shine
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think i have made a bit of process with this, just to clear up what it is i am trying to do is produce a list of words that are unique when they are sorted. But doing it in a way where i can keep the original word. So what i was trying to do is put the sorted word to check in a hashmap as a key and the the original word in the hashmap as the value, if it was unique take that word from the hashmap and produce a new list of unique words.


This is where i am at the minute but i now have two problems, for some reason contains? is not allowed to be used on a hash-map?? ive looked at the documents of hash-map and i cant seem to find anywhere that tells you have to run anything im used to like .contains .get .put

 
Sean Corfield
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing that's tripping you up here is that Clojure is expression-based, not statement-based like Java.

In particular, your transform2 function does not do what you think:


You need to think of your process as a transformation from a value (your collection of strings) to another value (a reduced collection of strings, in this case). Every single expression returns one value. Inside a function and inside a let you can have a series of expressions that all get evaluated but only the last expression's value is returned -- the others are assumed to be for side-effects only.

I highly recommend taking a step back and working through the exercises in the Clojure Koans and then at least some of 4clojure.com. You'll need to practice working with expression-based thinking, where functions transform values to new values without side effects.
 
Sean Corfield
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Freeman, here's something to look at that relates to what I was saying about reduce earlier and also ties into the problem you're trying to convert from Java:

This takes a collection of data (numbers) and computes two "running values" given a pair of initial values. Starting with an initial total of 0 and an empty set of values, it produces a pair of the overall total and the set of unique values in the collection:

This maps onto your problem because what you're trying to do is keep track of both a hash map and a set at the same time (so the initial values would be [{} #{}] for an empty hash map and an empty set). Hopefully you can see a parallel there?
reply
    Bookmark Topic Watch Topic
  • New Topic