<commit_before>import Foundation
import Alamofire
import AlamofireObjectMapper
import ObjectMapper

struct Story: ImmutableMappable {
    var id: Int
    var author: String
    var descendants: Int
    var kids: [Int]
    var score: Int
    var title: String
    var type: String
    var url: String

    init(map: Map) throws {
        id = try map.value("id")
        title = try map.value("title")
        score = try map.value("score")
        author = try map.value("by")
        type = try map.value("type")
        url = try map.value("url")
        descendants = (try? map.value("descendants")) ?? 0
        kids = (try? map.value("kids")) ?? []
    }

    mutating func mapping(map: Map) {
        id >>> map["id"]
        title >>> map["title"]
        score >>> map["score"]
        author >>> map["by"]
        type >>> map["type"]
        url >>> map["url"]
        descendants >>> map["descendants"]
        kids >>> map["kids"]
    }
}

enum Endpoint {
    case topStories
    case newStories
    case bestStories
    case showHN
    case askHN
    case jobs
    case updates
    case user(Int)
    case item(Int)

    var url: String {
        return "\(self.baseUrl)/\(self.path)"
    }

    var baseUrl: String {
        return "https://hacker-news.firebaseio.com/v0"
    }

    var path: String {
        switch self {
        case .topStories: return "/topstories.json"
        case .newStories: return "/newstories.json"
        case .bestStories: return "/beststories.json"
        case .showHN: return "/showstories.json"
        case .askHN: return "/askstories.json"
        case .jobs: return "/jobstories.json"
        case .updates: return "/updates.json"
        case .item(let id): return "/item/\(id).json"
        case .user(let id): return "/user/\(id).json"
        }
    }
}

func fetch<T: ImmutableMappable>(_ endpoint: Endpoint, onCompletion: @escaping (T) -> Void) {
    Alamofire.request(endpoint.url).validate().responseObject { (response: DataResponse<T>) in
        response.result.value.map(onCompletion)
    }
}

func fetch<T: ImmutableMappable>(_ endpoint: Endpoint, onCompletion: @escaping ([T]) -> Void) {
    Alamofire.request(endpoint.url).validate().responseArray { (response: DataResponse<[T]>) in
        response.result.value.map(onCompletion)
    }
}

func fetch<T>(_ endpoint: Endpoint, onCompletion: @escaping ([T]) -> Void) {
    Alamofire.request(endpoint.url).validate().responseJSON { response in
        if let json = response.result.value as? [T] {
            onCompletion(json)
        }
    }
}<commit_msg>Add fetch function for non mappable values<commit_after>import Foundation
import Alamofire
import AlamofireObjectMapper
import ObjectMapper

struct Story: ImmutableMappable {
    var id: Int
    var author: String
    var descendants: Int
    var kids: [Int]
    var score: Int
    var title: String
    var type: String
    var url: String

    init(map: Map) throws {
        id = try map.value("id")
        title = try map.value("title")
        score = try map.value("score")
        author = try map.value("by")
        type = try map.value("type")
        url = try map.value("url")
        descendants = (try? map.value("descendants")) ?? 0
        kids = (try? map.value("kids")) ?? []
    }

    mutating func mapping(map: Map) {
        id >>> map["id"]
        title >>> map["title"]
        score >>> map["score"]
        author >>> map["by"]
        type >>> map["type"]
        url >>> map["url"]
        descendants >>> map["descendants"]
        kids >>> map["kids"]
    }
}

enum Endpoint {
    case topStories
    case newStories
    case bestStories
    case showHN
    case askHN
    case jobs
    case updates
    case user(Int)
    case item(Int)

    var url: String {
        return "\(self.baseUrl)/\(self.path)"
    }

    var baseUrl: String {
        return "https://hacker-news.firebaseio.com/v0"
    }

    var path: String {
        switch self {
        case .topStories: return "/topstories.json"
        case .newStories: return "/newstories.json"
        case .bestStories: return "/beststories.json"
        case .showHN: return "/showstories.json"
        case .askHN: return "/askstories.json"
        case .jobs: return "/jobstories.json"
        case .updates: return "/updates.json"
        case .item(let id): return "/item/\(id).json"
        case .user(let id): return "/user/\(id).json"
        }
    }
}

func fetch<T: ImmutableMappable>(_ endpoint: Endpoint, onCompletion: @escaping (T) -> Void) {
    Alamofire.request(endpoint.url).validate().responseObject { (response: DataResponse<T>) in
        response.result.value.map(onCompletion)
    }
}

func fetch<T: ImmutableMappable>(_ endpoint: Endpoint, onCompletion: @escaping ([T]) -> Void) {
    Alamofire.request(endpoint.url).validate().responseArray { (response: DataResponse<[T]>) in
        response.result.value.map(onCompletion)
    }
}

func fetch<T>(_ endpoint: Endpoint, onCompletion: @escaping (T) -> Void) {
    Alamofire.request(endpoint.url).validate().responseJSON { response in
        (response.result.value as? T).map(onCompletion)
    }
}

func fetch<T>(_ endpoint: Endpoint, onCompletion: @escaping ([T]) -> Void) {
    Alamofire.request(endpoint.url).validate().responseJSON { response in
        (response.result.value as? [T]).map(onCompletion)
    }
}
{'fraction_non_alphanumeric': 0.1387434554973822, 'fraction_numerical': 0.0007479431563201197, 'mean_word_length': 2.5306930693069307, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 69, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2699517', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1648, 'n_words': 420}