Skip to content

Commit

Permalink
Add as-int.
Browse files Browse the repository at this point in the history
  • Loading branch information
amalloy committed Mar 26, 2012
1 parent 0a20d95 commit 998caf6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion project.clj
@@ -1,4 +1,4 @@
(defproject useful "0.8.0-alpha1"
(defproject useful "0.8.0-alpha2"
:description "A collection of generally-useful Clojure utility functions"
:dependencies [[org.clojure/clojure "1.4.0-beta1"]
[org.clojure/tools.macro "0.1.1"]]
Expand Down
8 changes: 8 additions & 0 deletions src/useful/datatypes.clj
@@ -1,10 +1,18 @@
(ns useful.datatypes
(:use [useful.map :only [position into-map update]]
[useful.utils :only [invoke]]
[useful.fn :only [fix]])
(:require [clojure.string :as s])
(:import (java.lang.reflect Field)
(clojure.lang Compiler$LocalBinding)))

(defn as-int [x]
(condp invoke x
integer? x
string? (Integer/parseInt x)
float? (int x)
nil))

(let [munge-ops [["?" "_QMARK_"]
["!" "_BANG_"]
["-" "_"]]
Expand Down
7 changes: 7 additions & 0 deletions test/useful/datatypes_test.clj
@@ -1,6 +1,13 @@
(ns useful.datatypes-test
(:use clojure.test useful.datatypes))

(deftest test-as-int
(are [in out] (= (as-int in) out)
"1" 1
2 2
4.5 4
nil nil))

(defrecord Test [a b c])
(defrecord Other [dash-thing question? bang!])
(record-accessors Test Other)
Expand Down

0 comments on commit 998caf6

Please sign in to comment.