Skip to content

Instantly share code, notes, and snippets.

@janherich
Last active January 2, 2016 05:19
Show Gist options
  • Save janherich/8255881 to your computer and use it in GitHub Desktop.
Save janherich/8255881 to your computer and use it in GitHub Desktop.
(->> data
(filter (comp #(re-find #"^([0-9.]+)\s%$" %) #(get % 5)))
(sort-by (comp #(read-string (second (re-find #"^([0-9.]+)\s%$" %))) #(get % 5)) >)
(into []))
@geoflen
Copy link

geoflen commented Jan 4, 2014

Hi, Janherich

Thanx a bunch mate,my data vector is generated by the function get-report-data, so how exactly do I map filter $ sort-by over it, sorry about this but very new to clojure and functional coding

@wodin
Copy link

wodin commented Jan 4, 2014

; Try this

(->> (get-report-data)
(filter (comp #(re-find #"^([0-9.]+)\s%$" %) #(get % 5)))
(sort-by #(read-string (second (re-find #"^([0-9.]+)\s%$" %))) >)
(into []))

@janherich
Copy link
Author

Hi Jeff,
As woodin suggested, just replace the data occurence with function call, or create a new function named for example transform data

(defn transform-data [data]
  (->> data
       (filter (comp #(re-find #"^([0-9.]+)\s%$" %) #(get % 5)))
       (sort-by (comp #(read-string (second (re-find #"^([0-9.]+)\s%$" %))) #(get % 5)) >)
       (into [])))

and then compose the function calls of the get-report-data and transform-data functions such as

(transform-data (get-report-data))

or use threading -> macro so the code is more readable and doesn't read 'inside-out'

(-> (get-report-data)
    (transform-data))

@geoflen
Copy link

geoflen commented Jan 5, 2014

Thanx a bunch guys let me give it a shot

@geoflen
Copy link

geoflen commented Jan 9, 2014

Hi Jan,

Decided to go with a vector of maps...but having issues sorting the maps can you please tell me why this does not sort well.....analysis of the output is not in the right order.thanks

(defn get-item-report-data-map
"Gets a vector of maps for all items data sorted by order-plan deviation"
([](reverse%28sort-by :orderplandev
%28remove %28fn [{orderplandev :orderplandev}] %28= orderplandev))
(apply vector
(map format-all-item-report-keyed
(item-repository/get-item-report-data)))))))

@janherich
Copy link
Author

Hi Jeff, i don't quite understand, do you mean that my function transform-data does not work with your new vector of maps data-structure ? Then you just need to tweak function transform-data to work with :keyword based retrieval functions instead of indices based, so in the function, instead of #(get % 5), you write just the keyword you use for deviation (this works because keywords are functions of maps you apply to them).
So if you for example have you deviation values mapped under :deviation key, the function definition will be:

(defn transform-data [data]
  (->> data
       (filter (comp #(re-find #"^([0-9.]+)\s%$" %) :deviation))
       (sort-by (comp #(read-string (second (re-find #"^([0-9.]+)\s%$" %))) :deviation) >)
       (into [])))

Or even better, pass the keyword to use as function parameter, so it becomes:

(defn transform-data [data deviation-key]
  (->> data
       (filter (comp #(re-find #"^([0-9.]+)\s%$" %) deviation-key))
       (sort-by (comp #(read-string (second (re-find #"^([0-9.]+)\s%$" %))) deviation-key) >)
       (into [])))

Then just call it:

(-> (get-report-data)
    (transform-data :deviation))

BTW, it's good decision to move from vectors to maps in this case, much better structure to hold this type of data.

@geoflen
Copy link

geoflen commented Jan 15, 2014

Thanks a bunch mate....

@geoflen
Copy link

geoflen commented Jan 16, 2014

Hi Jan......I guess yiu should bill me at the end of the month....buging you alot ....now I have this nested user data which looks like this

{"" {:username "", :password "$2a$10$ZdhEftfkQ61/k0GvXDaLGOtSRw5q6D3ZjfPdXx8GymrBxf3Bhr1fi", :roles #{:administrator}}, "" {:username "", :password "$2a$10$Au1gLG/5K/EJ0CPpBtMYLu7URjzkpyqv2wIC72oJows9I4xOcwlGa", :roles #{:user}},.......} ......have u worked with datatables...want to display username and role using a datatable like so....var oTable;
$(document).ready(function() {

$('#userslist').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
    $.getJSON("/home/user_list", function(json) {
    oTable = $('#example').dataTable( {
    "aaData": json,
    "aoColumns": [
        { "sTitle": "Role", "sClass": "center", "mdata":"roles"  }
        { "sTitle": "User Name", "sClass": "center", "mdata":"username"  }
    ],
    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
        /* Append the grade to the default row class name */
        var id = aData[0];


        return nRow;
    },
    "sDom": 'T<"clear">lfrtip',
"oTableTools": {
  "sSwfPath": "../datatables/swf/copy_csv_xls_pdf.swf"
}
} );

});

} );
but no luck yet...any Ideas please?

Cheers!

@geoflen
Copy link

geoflen commented Jan 16, 2014

: username and : password are actuall strings....sorry forgot to include them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment