| 918 | } |
| 919 | |
| 920 | static Object nthFrom(Object coll, int n){ |
| 921 | if(coll == null) |
| 922 | return null; |
| 923 | else if(coll instanceof CharSequence) |
| 924 | return Character.valueOf(((CharSequence) coll).charAt(n)); |
| 925 | else if(coll.getClass().isArray()) |
| 926 | return Reflector.prepRet(coll.getClass().getComponentType(),Array.get(coll, n)); |
| 927 | else if(coll instanceof RandomAccess) |
| 928 | return ((List) coll).get(n); |
| 929 | else if(coll instanceof Matcher) |
| 930 | return ((Matcher) coll).group(n); |
| 931 | |
| 932 | else if(coll instanceof Map.Entry) { |
| 933 | Map.Entry e = (Map.Entry) coll; |
| 934 | if(n == 0) |
| 935 | return e.getKey(); |
| 936 | else if(n == 1) |
| 937 | return e.getValue(); |
| 938 | throw new IndexOutOfBoundsException(); |
| 939 | } |
| 940 | |
| 941 | else if(coll instanceof Sequential) { |
| 942 | ISeq seq = RT.seq(coll); |
| 943 | coll = null; |
| 944 | for(int i = 0; i <= n && seq != null; ++i, seq = seq.next()) { |
| 945 | if(i == n) |
| 946 | return seq.first(); |
| 947 | } |
| 948 | throw new IndexOutOfBoundsException(); |
| 949 | } |
| 950 | else |
| 951 | throw new UnsupportedOperationException( |
| 952 | "nth not supported on this type: " + coll.getClass().getSimpleName()); |
| 953 | } |
| 954 | |
| 955 | static public Object nth(Object coll, int n, Object notFound){ |
| 956 | if(coll instanceof Indexed) { |