# ChooseYourBed
A helpful Elixir library for modeling and simulating bed selection scenarios, offering a structured way to represent preferences and outcomes. This package provides tools for evaluating different bed options based on predefined criteria.
## Installation
Add `choose_your_bed` to your list of dependencies in `mix.exs`:
elixir
def deps do
[
{:choose_your_bed, "~> 0.1.0"}
]
end
Then, run `mix deps.get` to fetch the dependencies.
## Usage Examples
Here are a few examples demonstrating how to use the `ChooseYourBed` library.
**Example 1: Simple Bed Evaluation**
elixir
alias ChooseYourBed
bed1 = %{comfort: 8, support: 7, price: 1000}
bed2 = %{comfort: 9, support: 6, price: 1200}
ChooseYourBed.evaluate_beds([bed1, bed2], comfort: 0.6, support: 0.4)
#=> {:ok, [%{comfort: 8, support: 7, price: 1000, score: 7.6}, %{comfort: 9, support: 6, price: 1200, score: 7.8}]}
**Example 2: Pattern Matching for Bed Selection**
elixir
alias ChooseYourBed
beds = [
%{name: "Cloud Nine", comfort: 9, support: 8, price: 1500},
%{name: "Firm Foundation", comfort: 6, support: 9, price: 1200},
%{name: "Budget Bliss", comfort: 7, support: 7, price: 800}
]
case ChooseYourBed.evaluate_beds(beds, comfort: 0.7, support: 0.3) do
{:ok, evaluated_beds} ->
best_bed = Enum.max_by(evaluated_beds, & &1[:score])
IO.puts "The best bed is: #{best_bed[:name]} with a score of #{best_bed[:score]}"
{:error, message} ->
IO.puts "Error: #{message}"
end
**Example 3: Using Pipes for Data Transformation**
elixir
alias ChooseYourBed
beds = [
%{name: "Luxury Dream", comfort: 10, support: 9, price: 2000},
%{name: "Standard Sleep", comfort: 7, support: 8, price: 1000}
]
beds
|> ChooseYourBed.evaluate_beds(comfort: 0.8, support: 0.2)
|> case do
{:ok, evaluated_beds} ->
Enum.sort_by(evaluated_beds, & &1[:score], :desc)
|> Enum.map(fn bed -> "Bed: #{bed[:name]}, Score: #{bed[:score]}" end)
|> Enum.each(&IO.puts/1)
{:error, message} ->
IO.puts "Error: #{message}"
end
**Example 4: Handling Invalid Input**
elixir
alias ChooseYourBed
beds = [%{comfort: 5, support: 5}] # Missing price
case ChooseYourBed.evaluate_beds(beds, comfort: 0.5, support: 0.5) do
{:ok, _} ->
IO.puts "Evaluation successful (this should not happen)"
{:error, message} ->
IO.puts "Error: #{message}" # Expected: "Each bed must have :price, :comfort, and :support keys"
end
**Example 5: Custom Evaluation Function**
elixir
alias ChooseYourBed
beds = [
%{name: "Memory Foam", comfort: 9, support: 7, temperature: "warm"},
%{name: "Latex", comfort: 8, support: 8, temperature: "cool"}
]
# Extend evaluation to consider temperature
defmodule CustomBedEvaluator do
import ChooseYourBed
def evaluate_beds(beds, comfort: comfort_weight, support: support_weight, temperature: temperature_pref) do
beds
|> Enum.map(fn bed ->
score = comfort_weight * bed[:comfort] + support_weight * bed[:support] +
case {bed[:temperature], temperature_pref} do
{"warm", "warm"} -> 1
{"cool", "cool"} -> 1
_ -> 0
end
Map.put(bed, :score, score)
end)
|> then(fn evaluated_beds -> {:ok, evaluated_beds} end)
end
end
case CustomBedEvaluator.evaluate_beds(beds, comfort: 0.4, support: 0.4, temperature: "cool") do
{:ok, evaluated_beds} ->
Enum.each(evaluated_beds, fn bed -> IO.puts "Bed: #{bed[:name]}, Score: #{bed[:score]}" end)
{:error, message} ->
IO.puts "Error: #{message}"
end
## Feature Summary
* Provides a structured way to represent bed characteristics.
* Offers a simple evaluation function based on weighted criteria.
* Supports pattern matching for easy bed selection based on evaluation results.
* Designed to be extensible and adaptable to custom evaluation logic.
* Provides error handling for invalid bed data.
## License
MIT
This package is part of the choose-your-bed ecosystem. For advanced features and enterprise-grade tools, visit: https://supermaker.ai/blog/how-to-make-the-viral-choose-your-bed-videos-with-ai/