<commit_before>//
//  Forecast+JSON.swift
//  Jetstream
//
//  Created by <NAME> on 11/20/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import CoreData

extension Forecast {
    class func forecasts(with json: JSON, in context: NSManagedObjectContext) -> [Forecast] {
        guard let daily = json["daily"] as? JSON else { return [] }
        guard let hourly = json["hourly"] as? JSON else { return [] }
        
        guard let dailyData = daily["data"] as? [JSON] else { return [] }
        guard let hourlyData = hourly["data"] as? [JSON] else { return [] }
        
        let weeklyForecasts = dailyData.flatMap { (obj) -> Forecast? in
            return dayForecast(with: obj, in: context)
        }
        
        let hourlyForecasts = hourlyData.flatMap { (obj) -> Forecast? in
            return hourlyForecast(with: obj, in: context)
        }
        
        let forecasts = weeklyForecasts + hourlyForecasts
        
        return forecasts
    }
    
    class func hourlyForecast(with json: JSON, in context: NSManagedObjectContext) -> Forecast? {
        guard
            let timestamp = json["time"] as? TimeInterval,
            let summary = json["summary"] as? String,
            let icon = json["icon"] as? String,
            let highTemp = json["temperature"] as? Double
//            let lowTemp = json["precipProbability"] as? Double<commit_msg>Change flatMap to compactMap<commit_after>//
//  Forecast+JSON.swift
//  Jetstream
//
//  Created by <NAME> on 11/20/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import CoreData

extension Forecast {
    class func forecasts(with json: JSON, in context: NSManagedObjectContext) -> [Forecast] {
        guard let daily = json["daily"] as? JSON else { return [] }
        guard let hourly = json["hourly"] as? JSON else { return [] }
        
        guard let dailyData = daily["data"] as? [JSON] else { return [] }
        guard let hourlyData = hourly["data"] as? [JSON] else { return [] }
        
        let weeklyForecasts = dailyData.compactMap { (obj) -> Forecast? in
            return dayForecast(with: obj, in: context)
        }
        
        let hourlyForecasts = hourlyData.compactMap { (obj) -> Forecast? in
            return hourlyForecast(with: obj, in: context)
        }
        
        let forecasts = weeklyForecasts + hourlyForecasts
        
        return forecasts
    }
    
    class func hourlyForecast(with json: JSON, in context: NSManagedObjectContext) -> Forecast? {
        guard
            let timestamp = json["time"] as? TimeInterval,
            let summary = json["summary"] as? String,
            let icon = json["icon"] as? String,
            let highTemp = json["temperature"] as? Double
//            let lowTemp = json["precipProbability"] as? Double
{'fraction_non_alphanumeric': 0.1119324181626188, 'fraction_numerical': 0.007039774727208729, 'mean_word_length': 2.251716247139588, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '3416991', 'n_tokens_mistral': 824, 'n_tokens_neox': 767, 'n_words': 252}