Skip to content
Browse files

Add as-int.

  • Loading branch information...
1 parent 0a20d95 commit 998caf6e9aeae742c249eb833662fa504b1a3cd3 @amalloy amalloy committed Mar 26, 2012
Showing with 16 additions and 1 deletion.
  1. +1 −1 project.clj
  2. +8 −0 src/useful/datatypes.clj
  3. +7 −0 test/useful/datatypes_test.clj
View
2 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"]]
View
8 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_"]
["-" "_"]]
View
7 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)

0 comments on commit 998caf6

Please sign in to comment.
Something went wrong with that request. Please try again.