def make_linear_mapper(in_range, out_range, int_out=False):
    """ make_linear_mapper returns a function which maps from the in_range to the out_range, and
    optionally converts to integers in the output if int_out = True. """

    in_delta = in_range[-1] - in_range[0]
    out_delta = out_range[-1] - out_range[0]
    # y = mx + b (m = slope, b = y-intersect)
    m = (out_delta / in_delta)
    # To find b: b = y - mx
    # Just plug in first item of each range
    b = out_range[0] - m * in_range[0]

    def linear_mapper(val):
        return m * val + b

    if int_out:
        mapper = lambda x: int(linear_mapper(x))
    else:
        mapper = linear_mapper

    return mapper